没有锁的std :: condition_variable

时间:2016-08-17 14:52:22

标签: c++ multithreading c++11 mutex

我正在尝试同步一组线程。这些线程大部分时间都在睡觉,醒来做他们的预定工作。我正在使用<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="container"> <div class="buttons"> <button class="grid">Grid View</button> <button class="list">List View</button> </div> <ul class="list_view"> <li> <div class="item1"> <img src="images\homepage-960px_26.jpg" alt="Shoe 1" /> <div class="switch"> <ul class="main_icons"> <li><i class="fa fa-eye" aria-hidden="true"></i> </li> <li><i class="fa fa-star-o" aria-hidden="true"></i> </li> <li><i class="fa fa-share-square-o" aria-hidden="true"></i> </li> <li><i class="fa fa-shopping-cart" aria-hidden="true"></i> </li> </ul> <p class="even_odd">Even&Odd</p> </div> </div> </li> <li> <div class="item2"> <img src="images\Item 2.png" alt="Shoe 2" /> <p class="on_sale">ON SALE!</p> <div class="switch" id="switch2"> <ul class="main_icons" id="main_icons2"> <li><i class="fa fa-eye" aria-hidden="true"></i> </li> <li><i class="fa fa-star-o" aria-hidden="true"></i> </li> <li><i class="fa fa-share-square-o" aria-hidden="true"></i> </li> <li><i class="fa fa-shopping-cart" aria-hidden="true"></i> </li> </ul> <p class="even_odd">Even&Odd</p> </div> </div> </li> <li> <div class="item3"> <img src="images\homepage-960px_29.jpg" alt="Shoe 3" /> <div class="switch" id="switch7"> <ul class="main_icons" id="main_icons7"> <li><i class="fa fa-eye" aria-hidden="true"></i> </li> <li><i class="fa fa-star-o" aria-hidden="true"></i> </li> <li><i class="fa fa-share-square-o" aria-hidden="true"></i> </li> <li><i class="fa fa-shopping-cart" aria-hidden="true"></i> </li> </ul> <p class="even_odd">Buffalo Decolettè</p> </div> </div> </li> <li> <div class="item4"> <img src="images\homepage-960px_33.jpg" alt="Shoe 4" /> <div class="switch" id="switch3"> <ul class="main_icons" id="main_icons3"> <li><i class="fa fa-eye" aria-hidden="true"></i> </li> <li><i class="fa fa-star-o" aria-hidden="true"></i> </li> <li><i class="fa fa-share-square-o" aria-hidden="true"></i> </li> <li><i class="fa fa-shopping-cart" aria-hidden="true"></i> </li> </ul> <p class="even_odd">Even&Odd</p> </div> </div> </li> <li> <div class="item5"> <img src="images\homepage-960px_34.jpg" alt="Shoe 5" /> <div class="switch" id="switch4"> <ul class="main_icons" id="main_icons4"> <li><i class="fa fa-eye" aria-hidden="true"></i> </li> <li><i class="fa fa-star-o" aria-hidden="true"></i> </li> <li><i class="fa fa-share-square-o" aria-hidden="true"></i> </li> <li><i class="fa fa-shopping-cart" aria-hidden="true"></i> </li> </ul> <p class="even_odd">Even&Odd</p> </div> </div> </li> <li> <div class="item6"> <img src="images\shoe_photo3.png" alt="Shoe 6" /> <p class="on_sale" id="out_stock">OUT OF <br/>&nbsp STOCK</p> <div class="switch" id="switch8"> <ul class="main_icons" id="main_icons8"> <li><i class="fa fa-eye" aria-hidden="true"></i> </li> <li><i class="fa fa-star-o" aria-hidden="true"></i> </li> <li><i class="fa fa-share-square-o" aria-hidden="true"></i> </li> <li><i class="fa fa-shopping-cart" aria-hidden="true"></i> </li> </ul> <p class="even_odd">Buffalo Decolettè</p> </div> </div> </li> <li> <div class="item7"> <img src="images\homepage-960px_26.jpg" alt="Shoe 7" /> <div class="switch" id="switch5"> <ul class="main_icons" id="main_icons5"> <li><i class="fa fa-eye" aria-hidden="true"></i> </li> <li><i class="fa fa-star-o" aria-hidden="true"></i> </li> <li><i class="fa fa-share-square-o" aria-hidden="true"></i> </li> <li><i class="fa fa-shopping-cart" aria-hidden="true"></i> </li> </ul> <p class="even_odd">Even&Odd</p> </div> </div> </li> <li> <div class="item8"> <img src="images\Item 2.png" alt="Shoe 8" /> <p class="on_sale" id="on_sale2">ON SALE!</p> <div class="switch" id="switch6"> <ul class="main_icons" id="main_icons6"> <li><i class="fa fa-eye" aria-hidden="true"></i> </li> <li><i class="fa fa-star-o" aria-hidden="true"></i> </li> <li><i class="fa fa-share-square-o" aria-hidden="true"></i> </li> <li><i class="fa fa-shopping-cart" aria-hidden="true"></i> </li> </ul> <p class="even_odd">Even&Odd</p> </div> </div> </li> <li> <div class="item9"> <img src="images\homepage-960px_29.jpg" alt="Shoe 3" /> <div class="switch" id="switch9"> <ul class="main_icons" id="main_icons9"> <li><i class="fa fa-eye" aria-hidden="true"></i> </li> <li><i class="fa fa-star-o" aria-hidden="true"></i> </li> <li><i class="fa fa-share-square-o" aria-hidden="true"></i> </li> <li><i class="fa fa-shopping-cart" aria-hidden="true"></i> </li> </ul> <p class="even_odd">Buffalo Decolettè</p> </div> </div> </li> </ul> </div>

不幸的是,当我终止应用程序线程阻止它退出时。在C#中,我可以将一个帖子设为std::thread,以便它在app退出时被终止。在我看来,在C ++中不能使用equavalint功能。

所以我决定使用一种事件指示器,并在应用程序退出时让线程唤醒。标准C ++ 11 background需要一个唯一的锁,所以我不能使用它,因为我需要两个线程同时唤醒(它们不共享任何资源)。

最终,我决定使用WinApi的std::condition_variable + CreateEvent + SetEvent来解决此问题。

我有一种方法可以使用c ++ 11实现相同的行为吗?

同样,我想要什么:

  1. 一组线程独立工作,通常都是睡着了 在特定时期内(对于不同的线程可能会有所不同;
  2. 所有threds检查一个可用于所有人的变量是否 这是停止工作的时候(我称之为变量WaitForSingleObject)。 实际上所有线程都在循环旋转,如下所示:

    IsAliva
  3. 线程必须能够同时工作,而不是相互阻塞;
  4. 当应用关闭并且事件上升并且它使线程到达时  现在醒来,无论超时;
  5. 醒来,它会检查 while (IsAlive) { // Do work std::this_thread::sleep_for(...); } 并退出。

2 个答案:

答案 0 :(得分:3)

是的,你可以使用条件变量的标准c ++机制,互斥体和某些标志来做到这一点

// Your class or global variables
std::mutex deathLock;
std::condition_variable deathCv;
bool deathTriggered = false;

// Kill Thread runs this code to kill all other threads:
{
    std::lock_guard<std::mutex> lock(deathLock);
    deathTriggered = true;
}
deathCv.notify_all();

// You Worker Threads run this code:
while(true)
{
   ... do work

   // Now wait for 1000 milliseconds or until death is triggered:
   std::unique_lock<std::mutex> lock(deathLock);
   deathCv.wait_for(lock, std::chrono::milliseconds(1000), [](){return deathTriggered;});

   // Check for death
   if(deathTriggered)
   {
      break;
   }
}

请注意,在进入条件之前触发死亡时,这会正确运行。您也可以使用wait_for的返回值,但这种方式更容易阅读imo。此外,虽然不清楚,多线程休眠是正常的,因为wait_for代码在内部解锁unique_lock同时休眠并重新检查它以检查条件以及何时返回。

最后,所有线程确实同时唤醒',因为它们在检查bool标志时被序列化,这只是为了几条指令,然后当它们突破循环时解锁。这将是不明显的。

答案 1 :(得分:0)

在c ++ 11中,您应该能够[WARN] Nested component 'contactEmailsTable' Nested component has relative height 100.0% inside window with undefined height 一个线程,这样它就会被视为一个守护程序线程,这意味着如果该应用程序终止,该线程将自动停止。