Boost :: lifetime lifetime - should it stay alive after all threads have reached wait ()?

Consider the following:

#include <boost/thread.hpp>

boost::thread spawn() {
    boost::barrier barrier{2};
    boost::thread ret{[&barrier] {
        // some initialization code  
        barrier.wait();
        // actual thread code
    }};

    barrier.wait();
    return ret;
}

The function spawncreates an object boost::barrieron the stack, generates a new thread, and then waits until it reaches the barrier. The goal is that the function spawnwants to wait until the spawned thread is actually started and initialized before returning control.

After a new thread reaches the barrier, the function spawnresumes, destroys the barrier object and returns. It looks pretty neat.

Is the approach presented safe? If not, what is the recommended way to achieve the desired behavior? (I think I could hold the barrier under shared_ptr, but it somehow doesn't seem to be the right way.)


:

  • wait
  • wait ( wait )
  • wait
  • wait (, ), , .

, , , , wait "" (.. , ) - ?

Boost :

count-th , reset, .

, , , - ?

+4

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


All Articles