Eagle
Eagle

Reputation: 3474

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:

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

Upvotes: 0

Views: 341

Answers (3)

Andriy Tylychko
Andriy Tylychko

Reputation: 16256

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.

Upvotes: 2

Elalfer
Elalfer

Reputation: 5338

You should use mutex and/ro semaphore to synchronize your threads and lock variable to achieve thread-safe communication. Just note that all threads in your process share the same memory so you can access the same data, but you have to do it in a thread-safe way.

I'm not sure if boost library implements any threading primitives, but here is a good tutorial about multi-threading programming using POSIX threads - http://www.yolinux.com/TUTORIALS/LinuxTutorialPosixThreads.html

Upvotes: 0

stefaanv
stefaanv

Reputation: 14392

A proven way to control a thread from another thread is to send messages via a combination of a queue with a conditional variable. Unfortunately, boost::thread doesn't provide a standard solution and there are a couple of tricky things when implementing (possible deadlocks, behaviour when queue is full, use polymorphic messages...)

Upvotes: 0

Related Questions