简体   繁体   English

通过多个线程提升条件变量

[英]Boost Condition Variable with multiple threads

In my program, I have two basic threads. 在我的程序中,我有两个基本线程。 The first one is the main thread and the second is a Tcp server thread. 第一个是主线程,第二个是Tcp服务器线程。 The TCP server will listen for requests ,and for each request it will create a corresponding thread, each of the newly created threads should start working until they reach a certain point where they have to wait for an indication from the main thread. TCP服务器将侦听请求,并针对每个请求创建一个相应的线程,每个新创建的线程应开始工作,直到到达某个点为止,在该点上它们必须等待主线程发出的指示。 To solve this issue I am implementing a condition variable using Boost 1.49. 为了解决这个问题,我正在使用Boost 1.49实现条件变量。

My main problem is whenever any of the newly created threads reach the point of the condition variable my whole program freezes. 我的主要问题是,每当新创建的线程到达条件变量的位置时,我的整个程序就会冻结。

For more information, please check: Boost 1.49 Condition Variable issue 有关更多信息,请检查: Boost 1.49条件变量问题

Until now I didn't receive any positive response, and I am not able to solve the problem. 到目前为止,我还没有收到任何积极的回应,也无法解决问题。

Thanks a lot. 非常感谢。

I haven't looked at your other question (too much code) 我没有看过您的其他问题(代码太多)

In general, you have to await/signal a condition under the corresponding mutex, though. 通常,您必须在相应的互斥锁下等待/发出信号。

Here's a demonstration using a group of 10 workers that await a start signal: 这是一个演示,使用了一组由10个工人组成的等待启动信号的示例:

  • See it Live On Coliru 在Coliru上实时观看

     #include <boost/thread.hpp> #include <boost/optional/optional_io.hpp> ///////////////////////// // start condition logic boost::mutex mx; boost::condition_variable cv; static bool ok_to_start = false; void await_start_condition() { boost::unique_lock<boost::mutex> lk(mx); cv.wait(lk, [] { return ok_to_start; }); } void signal_start_condition() { boost::lock_guard<boost::mutex> lk(mx); ok_to_start = true; cv.notify_all(); } ///////////////////////// // workers static boost::optional<int> shared_secret; void worker(int id) { await_start_condition(); // demo worker implementation static boost::mutex console_mx; boost::lock_guard<boost::mutex> lk(console_mx); std::cout << "worker " << id << ": secret is " << shared_secret << "\\n"; } int main() { boost::thread_group threads; for (int i = 0; i<10; i++) threads.create_thread(boost::bind(worker, i)); // demo - initialize some state before thread start shared_secret = 42; // signal threads can start signal_start_condition(); // wait for all threads to finish threads.join_all(); } 
  • In case of C++03 you can replace the lambda with a hand-written predicate: Live On Coliru 如果是C ++ 03,则可以用手写谓词代替lambda: Live On Coliru

     namespace /* anon detail */ { bool ok_to_start_predicate() { return ok_to_start; } } void await_start_condition() { boost::unique_lock<boost::mutex> lk(mx); cv.wait(lk, ok_to_start_predicate); } 
  • Or you can use Boost Lambda/Boost Phoenix to do the trick for you: Live On Coliru 或者,您可以使用Boost Lambda / Boost Phoenix帮您解决问题: Live On Coliru

     #include <boost/phoenix.hpp> void await_start_condition() { boost::unique_lock<boost::mutex> lk(mx); cv.wait(lk, boost::phoenix::cref(ok_to_start)); } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM