<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Mal wieder Thread schlafen legen]]></title><description><![CDATA[<p>Hallo zusammen,</p>
<p>ich möchte einen thread 'a' von thread 'b' aus schlafen legen.<br />
Im Fall, dass 'a' nicht schlafen muss, soll die Überprüfung lockfrei gehen.</p>
<p>Was haltet ihr von folgender Implementierung:</p>
<pre><code>class Event
{
public:
  Event() : mCond(false) {}

  void Set()
  {
    std::mutex::scoped_lock lock(mMutex);
    mCond = true;
    mCondVar.notify_all();
  }

  void WaitReset()
  {
    std::unique_lock&lt;std::mutex&gt; lock(mMutex);
    while (!mCond) mCondVar.wait(lock);
    mCond = false;
  }

private:
  std::mutex              mMutex;
  std::condition_variable mCondVar;
  bool                    mCond;
};

class SleepFlag
{
public:  
  SleepFlag() : mSleep(false) {}

  // called by thread a
  void Check()
  {
    if (mSleep) {
      mWakeUp.WaitReset();
    }
  }

  // called by thread b
  void Sleep()
  {
    mSleep = true;
  }

  void WakeUp()
  {
    mSleep = false;
    mWakeUp.Set();
  }

private:
  std::atomic&lt;bool&gt; mSleep;
  Event             mWakeUp;
};
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/324501/mal-wieder-thread-schlafen-legen</link><generator>RSS for Node</generator><lastBuildDate>Tue, 14 Jul 2026 08:40:14 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/324501.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 19 Mar 2014 17:05:46 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Mal wieder Thread schlafen legen on Wed, 19 Mar 2014 17:05:46 GMT]]></title><description><![CDATA[<p>Hallo zusammen,</p>
<p>ich möchte einen thread 'a' von thread 'b' aus schlafen legen.<br />
Im Fall, dass 'a' nicht schlafen muss, soll die Überprüfung lockfrei gehen.</p>
<p>Was haltet ihr von folgender Implementierung:</p>
<pre><code>class Event
{
public:
  Event() : mCond(false) {}

  void Set()
  {
    std::mutex::scoped_lock lock(mMutex);
    mCond = true;
    mCondVar.notify_all();
  }

  void WaitReset()
  {
    std::unique_lock&lt;std::mutex&gt; lock(mMutex);
    while (!mCond) mCondVar.wait(lock);
    mCond = false;
  }

private:
  std::mutex              mMutex;
  std::condition_variable mCondVar;
  bool                    mCond;
};

class SleepFlag
{
public:  
  SleepFlag() : mSleep(false) {}

  // called by thread a
  void Check()
  {
    if (mSleep) {
      mWakeUp.WaitReset();
    }
  }

  // called by thread b
  void Sleep()
  {
    mSleep = true;
  }

  void WakeUp()
  {
    mSleep = false;
    mWakeUp.Set();
  }

private:
  std::atomic&lt;bool&gt; mSleep;
  Event             mWakeUp;
};
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2389774</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2389774</guid><dc:creator><![CDATA[C14]]></dc:creator><pubDate>Wed, 19 Mar 2014 17:05:46 GMT</pubDate></item><item><title><![CDATA[Reply to Mal wieder Thread schlafen legen on Wed, 19 Mar 2014 18:34:24 GMT]]></title><description><![CDATA[<p>Wozu soll denn das gut sein?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2389805</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2389805</guid><dc:creator><![CDATA[TyRoXx]]></dc:creator><pubDate>Wed, 19 Mar 2014 18:34:24 GMT</pubDate></item><item><title><![CDATA[Reply to Mal wieder Thread schlafen legen on Wed, 19 Mar 2014 20:56:18 GMT]]></title><description><![CDATA[<p>Ich würde die beiden Klassen zu einer zusammenfassen, und dann &quot;mCond&quot; und &quot;mSleep&quot; zu einer Variable zusammenfassen.<br />
Dadurch ergibt sich das klassische &quot;double checked locking&quot; - was dank <code>std::atomic</code> in diesem Fall auch &quot;sicher&quot; ist.</p>
<p>So wie du es geschrieben hast, könnte es nämlich Probleme geben, sobald es mehr als einen &quot;Thread A&quot; gibt.</p>
<p>Was mMn. auch überhaupt keinen Sinn macht, ist dass du einen &quot;auto reset event&quot; implementierst, und dabei <code>notify_all</code> verwendest.<br />
Der zweite Thread der aufwacht sieht ja bereits wieder <code>mCond == false</code> , und legt sich daher gleich wieder heia. Wozu ihn dann überhaupt aufwecken?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2389833</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2389833</guid><dc:creator><![CDATA[hustbaer]]></dc:creator><pubDate>Wed, 19 Mar 2014 20:56:18 GMT</pubDate></item><item><title><![CDATA[Reply to Mal wieder Thread schlafen legen on Thu, 20 Mar 2014 14:10:10 GMT]]></title><description><![CDATA[<p>Ok, danke für den Tipp hustbaer!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2389955</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2389955</guid><dc:creator><![CDATA[C14]]></dc:creator><pubDate>Thu, 20 Mar 2014 14:10:10 GMT</pubDate></item></channel></rss>