4

I am using boost::thread to process messages in a queue. When a first message comes I start a message processing thread. When a second message comes I check if the message processing thread is done.

if it is done I start a new one if it is not done I don nothing.

How do I know if the thread is done ? I tried with joinable() but it is not working, as when the thread is done, it is still joinable.

I also tried to interrupt the process at once, and add an interruption point at the end of my thread, but it did not work.

Thanks

EDIT :

I would like to have my thread sleep for an undetermined time, and wake up when a signal is triggered.

The mean to do it is boost::condition_variable

0x26res
  • 11,925
  • 11
  • 54
  • 108

3 Answers3

2

As far as I know you should use the join() method to wait the end of a thread execution. You can use it with a timeout with timed_join().

You can interrupt threads with interrupt(). In this case, inside the thread an exception will occur if the execution reaches an interruption point ( a boost::this_thread::sleep() or boost::this_thread::interruption_point() ). You catch the exception inside the thread and you can then close it.

Nikko
  • 4,182
  • 1
  • 26
  • 44
1

Spawning a new thread for each incoming message is very inefficient. You should check out the Thread pool pattern.

EDIT:

Sorry, jules, I misread your question. I recommend you take a look at the producer-consumer pattern. Check out this article on how to roll your own blocking queue using boost condition variables. Intel's Thread Building Blocks also has a blocking queue implementation.

Check out this SO question about existing lock-free queue implementations.

Hope this helps.

Community
  • 1
  • 1
Emile Cormier
  • 28,391
  • 15
  • 94
  • 122
  • My idea is to have only one processing thread. When it is done processing, I would like it to sleep, and to be able to wake it up as soon as a new message comes. Do you know how to do that with boost ? – 0x26res May 11 '10 at 06:36
  • You'll want a producer-consumer queue. See my addendum above. – Emile Cormier May 11 '10 at 14:25
  • I understand well the producer-consumer pattern. What I need is a way to wake up a thread on an event (when the buffer is not empty any more). I don't know what are the means to do this. – 0x26res May 12 '10 at 14:46
  • The link to the article I posted in my answer shows how to use a Boost condition variable to wait for an event (queue not empty). You may also want to check out Boost's documentation here: http://www.boost.org/doc/libs/release/doc/html/thread/synchronization.html#thread.synchronization.condvar_ref – Emile Cormier May 14 '10 at 20:33
0

Have you tried checking get_id() with boost::this_thread::get_id(). If they match the thread does not exist. But that will only happen if you have exited the thread.

jpyllman
  • 307
  • 2
  • 6
  • The thread IDs are always different even when the thread is done. How can I exit the thread ? – 0x26res May 10 '10 at 09:45
  • By exit the thread I just mean it should come to the point where the thread stops and return. But I guess joinable() do the same thing. – jpyllman May 10 '10 at 09:53