views:

261

answers:

1

hi.

I am trying to accomplish something like this:

thread t; // create/initialize 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

+3  A: 

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.

Daniel Earwicker
this is in alternative.However in my case work queue is somewhat complicated. It would be much simpler if I have master thread dispensing work (which is what I am trying to do)
aaa
@aaa - see update; I can't see how you could do anything simpler than this unless you get rid of threads altogether.
Daniel Earwicker
work structure is a nested loops with weird bounds and not all threads can handle all work. I did however manage to implement flat task queue with backlog. this maybe more scalable approach in end
aaa