@@ -44,44 +44,47 @@ class CThreadPool
4444 auto enqueue (F&& f, Args&&... args)
4545 -> std::future<typename std::result_of<F(Args...)>::type>;
4646 ~CThreadPool ();
47+
4748private:
4849 // need to keep track of threads so we can join them
49- std::vector< std::thread > workers;
50+ std::vector< std::thread > workers;
5051 // the task queue
5152 std::queue< std::function<void ()> > tasks;
5253
5354 // synchronization
54- std::mutex queue_mutex;
55- std::condition_variable condition;
56- bool stop;
55+ std::mutex queue_mutex;
56+ std::condition_variable condition;
57+ bool stop;
5758};
5859
5960// the constructor just launches some amount of workers
6061inline CThreadPool::CThreadPool (size_t threads)
6162 : stop(false )
6263{
63- for (size_t i = 0 ;i<threads;++i)
64- workers.emplace_back (
65- [this ]
66- {
67- for (;;)
64+ for ( size_t i = 0 ; i < threads; ++i ) {
65+ workers.emplace_back ( [this ] {
66+ for (;;)
6867 {
6968 std::function<void ()> task;
7069
7170 {
72- std::unique_lock<std::mutex> lock (this ->queue_mutex );
73- this ->condition .wait (lock,
74- [this ]{ return this ->stop || !this ->tasks .empty (); });
75- if (this ->stop && this ->tasks .empty ())
71+ std::unique_lock<std::mutex> lock ( this ->queue_mutex );
72+ this ->condition .wait ( lock,
73+ [this ] { return this ->stop || !this ->tasks .empty (); } );
74+
75+ if ( this ->stop && this ->tasks .empty () ) {
7676 return ;
77- task = std::move (this ->tasks .front ());
77+ }
78+
79+ task = std::move ( this ->tasks .front () );
7880 this ->tasks .pop ();
7981 }
8082
8183 task ();
8284 }
8385 }
8486 );
87+ }
8588}
8689
8790// add new work item to the pool
@@ -91,19 +94,19 @@ auto CThreadPool::enqueue(F&& f, Args&&... args)
9194{
9295 using return_type = typename std::result_of<F (Args...)>::type;
9396
94- auto task = std::make_shared< std::packaged_task<return_type ()> >(
97+ auto task = std::make_shared< std::packaged_task<return_type ()> > (
9598 std::bind (std::forward<F>(f), std::forward<Args>(args)...)
9699 );
97100
98101 std::future<return_type> res = task->get_future ();
99102 {
100- std::unique_lock<std::mutex> lock ( queue_mutex);
103+ std::unique_lock<std::mutex> lock ( queue_mutex );
101104
102105 // don't allow enqueueing after stopping the pool
103- if ( stop)
104- throw std::runtime_error (" enqueue on stopped CThreadPool" );
106+ if ( stop )
107+ throw std::runtime_error ( " enqueue on stopped CThreadPool" );
105108
106- tasks.emplace ( [task]() { (*task)(); });
109+ tasks.emplace ( [task] () { (*task)(); } );
107110 }
108111 condition.notify_one ();
109112 return res;
@@ -113,11 +116,11 @@ auto CThreadPool::enqueue(F&& f, Args&&... args)
113116inline CThreadPool::~CThreadPool ()
114117{
115118 {
116- std::unique_lock<std::mutex> lock ( queue_mutex);
119+ std::unique_lock<std::mutex> lock ( queue_mutex );
117120 stop = true ;
118121 }
119122 condition.notify_all ();
120- for ( std::thread &worker: workers)
123+ for ( std::thread &worker: workers )
121124 worker.join ();
122125}
123126
0 commit comments