add original source
This commit is contained in:
53
source/listing_9.1.cpp
Normal file
53
source/listing_9.1.cpp
Normal file
@@ -0,0 +1,53 @@
|
||||
class thread_pool
|
||||
{
|
||||
std::atomic_bool done;
|
||||
thread_safe_queue<std::function<void()> > work_queue;
|
||||
std::vector<std::thread> threads;
|
||||
join_threads joiner;
|
||||
|
||||
void worker_thread()
|
||||
{
|
||||
while(!done)
|
||||
{
|
||||
std::function<void()> task;
|
||||
if(work_queue.try_pop(task))
|
||||
{
|
||||
task();
|
||||
}
|
||||
else
|
||||
{
|
||||
std::this_thread::yield();
|
||||
}
|
||||
}
|
||||
}
|
||||
public:
|
||||
thread_pool():
|
||||
done(false),joiner(threads)
|
||||
{
|
||||
unsigned const thread_count=std::thread::hardware_concurrency();
|
||||
try
|
||||
{
|
||||
for(unsigned i=0;i<thread_count;++i)
|
||||
{
|
||||
threads.push_back(
|
||||
std::thread(&thread_pool::worker_thread,this));
|
||||
}
|
||||
}
|
||||
catch(...)
|
||||
{
|
||||
done=true;
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
~thread_pool()
|
||||
{
|
||||
done=true;
|
||||
}
|
||||
|
||||
template<typename FunctionType>
|
||||
void submit(FunctionType f)
|
||||
{
|
||||
work_queue.push(std::function<void()>(f));
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user