How do I know if a boost thread is running?

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

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

How do I know if a thread is running? I tried with joinable (), but it does not work, since when the thread executes, it is still available for connection.

I also tried to abort the process right away and add a breakpoint at the end of my thread, but that didn't work.

thanks

EDIT:

I want my thread to sleep indefinitely and wake up when a signal is triggered.

Average value - boost :: condition_variable

+4
source share
3 answers

The appearance of a new thread for each incoming message is very inefficient. You should check the pool pool template .

EDIT:

Sorry, Jules, I misunderstood your question. I recommend you take a look at the producer-consumer pattern. Check out this article on how to collapse your own blocking queue using forced state variables. Intel Thread Blocking also has a blocking queue implementation.

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

Hope this helps.

+1
source

As far as I know, you should use the join () method to wait for the thread to finish executing. You can use it with a timeout with timed_join ().

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

+2
source

You tried to check get_id () with boost :: this_thread :: get_id (). If they match the stream, they do not exist. But this will only happen if you exit the stream.

0
source

Source: https://habr.com/ru/post/1309295/


All Articles