How to remove a stream boost object when the thread itself terminates?

When threads are added to boost :: thread_group, for example:

boost::thread_group my_threads;
boost::thread *t = new boost::thread( &someFunc );
my_threads.add_thread(th);

all boost :: thread objects created are deleted only when the object my_threadsgoes out of scope. But my main program thread generates many threads at runtime. Therefore, if about 50 threads have already been made, the program uses 1.5 GB , and this memory is freed only when the main process is completed.

Question: How to remove this boost :: thread object when the thread function is finished ?!

+5
source share
2 answers

, , ( boost:: thread_group , , ):

void someFunc(..., boost::thread_group & thg, boost::thread * thisTh)
{
  // do sth

  thg.remove_thread(thisThr);
  delete thisTh; // we coud do this as thread of execution and boost::thread object are quite independent
}

void run()
{
  boost::thread_group my_threads;
  boost::thread *t = new boost::thread(); // invalid handle, but we need some memory placeholder, so we could pass it to someFunc
  *t = boot::thread(
    boost::bind(&someFunc, boost::ref(my_threads), t)
  );
  my_threads.add_thread(t);
  // do not call join
}

at_thread_exit().

, boost:: thread objects 30 .

+6

:

* t = boot :: thread (boost :: bind (& someFunc, boost :: ref (my_threads), t));

. . , ////sth , ?

0

All Articles