c++ - boost::threadpool::pool vs.boost::thread_group -
i'm trying understand different use cases. , difference between 2 thread uses. this great tutorial have read explains boost::thread_group
.
and here code i'm using:
boost::threadpool::pool s_threadpool(getcorecount()); cfiltertask task(pfilter, // filter run boost::bind(&cfiltermanagerthread::oncompletetask, this, _1, _2) // oncomplete sync callback // _1 filter name // _2 error code ); // schedule new task - runs on threadpool s_threadpool.schedule(task);
this destructor:
s_threadpool.wait(0);
can please explain?
boost::thread_group
convenience class performing thread management operations on collection of threads. example, instead of having iterate on std::vector<boost::thread>
, invoking join()
on each thread, thread_group
provides convenient join_all()
member function.
with boost::thread
, regardless of being managed boost::thread_group
, lifetime of thread dependent on work in thread doing. example, if thread created perform computationally expensive calculation, thread can exit once result has been calculated. if work short-lived, overhead of creating , destroying threads can affect performance.
on other hand, threadpool pattern, number of threads services number of task/work. lifetime of thread not directly associated lifetime of task. continue previous example, application schedule computationally expensive calculation run within thread pool. work queued within threadpool, , 1 of threadpool's threads selected perform work. once calculation has completed, thread goes waiting more work scheduled threadpool.
as shown in threadpool example, threadpool can implemented boost::thread_group
manage lifetime of threads, , boost::asio::io_service
task/work dispatching.
Comments
Post a Comment