c++ - Control multithreaded flow with condition_variable -


I have not yet wrapped my head around the C ++ 11 multithreading stuff, but I have tried several threads Wait till some event on the main thread and then all continue in one time (processed what happened), and wait re-process it ... until they are closed Pets. Not at all - this is a simple breeding of my problem:

  std :: mutex mutex; Std :: condition_variable cv; Std :: thread thread1 ([& amp;] () {std :: unique_lock  lock (mutex); cv.wait (lock); std :: cout & lt; & lt; "GO1! \ N";}); Std :: thread thread2 ([& amp;] () {std :: unique_lock  lock (mutex); cv.wait (lock); std :: cout & lt; & lt; "GO2! \ N";}); Cv.notify_all (); // something happened - thread can now process this thread1.join (); Thread2.join ();   

It works ... as long as I do not close some breakpoints and slower things. When I do this, let me go to Go1! appears and then waiting for thread2 for cv.wait . What's wrong

Maybe I should not use any condition variable ... there is no condition around waiting , nor need to keep that data safe Is mutex What should I be doing instead of this?

You are on the right track ...

  std :: mutex mutex; Std :: condition_variable cv; Bool go = false; Std :: thread thread1 ([& amp;] () {std :: unique_lock & lt; std :: mutex & gt; lock (mutex); while (go!) Cv.wait (lock); std :: cout & Lt; & lt; "GO1! \ N";}); Std :: Thread Thread 2 ([& amp;] () {std :: unique_lock  lock (mutex); while (go!) Cv.wait (lock); std :: cout & Lt; & lt; "GO2! \ N";}); {Std :: unique_lock & lt; Std :: mutex & gt; (Mutex) lock; Go = true; Cv.notify_all (); // something happened - threads can now process it) thread1.join (); Thread2.join ();    

Comments

Popular posts from this blog

Python SQLAlchemy:AttributeError: Neither 'Column' object nor 'Comparator' object has an attribute 'schema' -

java - How not to audit a join table and related entities using Hibernate Envers? -

mongodb - CakePHP paginator ignoring order, but only for certain values -