开发者

C++ boost thread reusing threads

I am trying to accomplish something like this:

thread t; // create/initialize 开发者_C百科thread
t.launch(); // launch thread.
t.wait(); // wait
t.launch(); // relaunch the same thread

How to go about implementing something like this using boost threads? in essence, I need persistent relaunch-able thread.

I would like to avoid work queue, as implemented in my case is somewhat difficult

Thanks


You would just make the thread run in a loop. It attempts to take a unit of "work" from a queue, carries out the work, and then goes back to the queue. When the queue is empty it waits.

Then from another thread you can insert work items into the queue so that the thread will carry them out.

Reading your question again, are you saying that you want your master thread to notify the worker thread to begin working, but then the master thread must immediately begin waiting for the worker to finish? This would mean that only one thread is ever running at a time. There would be no point to that. Threads are designed for concurrent execution.

Assuming that isn't what you want, then I'm wondering what could be simpler than a simple worker thread running off a queue. A thread is either working or waiting. So you need some data structure that allows communication between master and worker, such that the worker can wait for a work item to arrive and the master can send a work item, which will wake up the worker, and then when the work item is completed, the worker waits for another one.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜