Search code examples
c++boostboost-thread

passing variable to a thread after it already started


i am newbie in C++ and boost.

As part of my master thesis, i wrote a program which simulate a statistical model. During the computation, i use boost::thread to process my "center of mass vector", for saving some computation time. So far so good.

Now, i would like to take each result from the boost::thread (each time one element) and pass it to a running thread, which is going to preform recursive regression.

My questions:

  • how can i pass my new computed element to the existing thread?
  • how could i "wake-up" the thread, when i pass the new element?

i would be happy if someone could point me to an existing example.


Solution

  • the simplest possible way is to use std::queue, boost::mutex and boost::conditional_variable. wrap any access to queue by mutex, after pushing to queue call conditional_variable.notify_one(). in consumer thread wait on conditional_variable until any result is ready, then process it.