File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -85,7 +85,13 @@ void ThreadPool::doWork()
8585 task = workQueue.front ();
8686 workQueue.pop ();
8787 }
88-
88+ ++busy;
8989 task ();
90+ --busy;
9091 }
9192}
93+
94+ void ThreadPool::waitFinished () {
95+ std::unique_lock<std::mutex> g (workQueueMutex);
96+ workQueueConditionVariable.wait (g, [&]{ return workQueue.empty () && (busy == 0 ); });
97+ }
Original file line number Diff line number Diff line change 88#ifndef ThreadPool_hpp
99#define ThreadPool_hpp
1010
11- #include < stdio.h>
12- #include < thread>
13- #include < mutex>
1411#include < condition_variable>
1512#include < exception>
16- #include < vector >
13+ #include < mutex >
1714#include < queue>
15+ #include < stdio.h>
16+ #include < thread>
17+ #include < vector>
1818
19- class ThreadPool
20- {
19+ class ThreadPool {
2120public:
2221 ThreadPool ();
2322 ~ThreadPool ();
2423 void queueWork (std::function<void (void )> task);
25-
24+ void waitFinished ();
25+
2626private:
27+ unsigned int busy;
2728 // This condition variable is used for the threads to wait until there is work
2829 // to do
2930 std::condition_variable_any workQueueConditionVariable;
@@ -40,7 +41,7 @@ class ThreadPool
4041 // This will be set to true when the thread pool is shutting down. This tells
4142 // the threads to stop looping and finish
4243 bool done;
43-
44+
4445 // Function used by the threads to grab work from the queue
4546 void doWork ();
4647};
You can’t perform that action at this time.
0 commit comments