Skip to content

Commit 5ca980b

Browse files
refactored simple_threadpool
added unittest for simple_threadpool
1 parent cf85fd0 commit 5ca980b

3 files changed

Lines changed: 22 additions & 34 deletions

File tree

example/taskflow_subsum.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ void maxSubArraySum(std::vector<int>& vec, int l, int r, std::atomic<int>& max_n
7272

7373
// Function: main
7474
int main(int argc, char* argv[]) {
75+
7576
::srand(1);
7677

7778
std::vector<int> vec(total, 0);

taskflow/threadpool/simple_threadpool.hpp

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -232,39 +232,6 @@ auto SimpleThreadpool::async(C&& c, Signal sig) {
232232
}
233233

234234
// Procedure: wait_for_all
235-
// After this method returns, all previously-scheduled tasks in the pool
236-
// will have been executed.
237-
//inline void SimpleThreadpool::wait_for_all() {
238-
//
239-
// if(is_worker()) {
240-
// throw std::runtime_error("Worker thread cannot wait for all");
241-
// }
242-
//
243-
// std::mutex barrier_mutex;
244-
// std::condition_variable barrier_cv;
245-
// auto threads_to_sync{_threads.size()};
246-
// std::vector<std::future<void>> futures;
247-
//
248-
// for(size_t i=0; i<_threads.size(); ++i) {
249-
// futures.emplace_back(async([&] () {
250-
// std::unique_lock<std::mutex> lock(barrier_mutex);
251-
// if (--threads_to_sync; threads_to_sync == 0) {
252-
// barrier_cv.notify_all();
253-
// }
254-
// else {
255-
// barrier_cv.wait(lock, [&threads_to_sync] {
256-
// return threads_to_sync == 0;
257-
// });
258-
// }
259-
// }));
260-
// }
261-
//
262-
// // Wait for all threads to have finished synchronization
263-
// for (auto& fu : futures) {
264-
// fu.get();
265-
// }
266-
//}
267-
268235
inline void SimpleThreadpool::wait_for_all(){
269236

270237
if(is_worker()) {
@@ -275,7 +242,6 @@ inline void SimpleThreadpool::wait_for_all(){
275242
_wait_for_all = true;
276243
_complete.wait(lock, [this](){ return _idle_workers == num_workers() && _task_queue.size() == 0; });
277244
_wait_for_all = false;
278-
279245
}
280246

281247
// Procedure: shutdown

unittest/threadpool.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,13 @@ void test_dynamic_tasking(T& threadpool) {
229229
TEST_CASE("SimpleThreadpool" * doctest::timeout(300)) {
230230

231231
const size_t num_tasks = 100;
232+
233+
//SUBCASE("Ownership") {
234+
// for(unsigned i=0; i<=4; ++i) {
235+
// tf::SimpleThreadpool tp(i);
236+
// test_ownership(tp);
237+
// }
238+
//}
232239

233240
SUBCASE("PlaceTask"){
234241
for(unsigned i=0; i<=4; ++i) {
@@ -244,6 +251,20 @@ TEST_CASE("SimpleThreadpool" * doctest::timeout(300)) {
244251
test_wait_for_all(tp);
245252
}
246253
}
254+
255+
SUBCASE("SpawnShutdown") {
256+
for(unsigned i=0; i<=4; ++i) {
257+
tf::SimpleThreadpool tp(i);
258+
test_spawn_shutdown(tp);
259+
}
260+
}
261+
262+
SUBCASE("DynamicTasking") {
263+
for(unsigned i=0; i<=4; ++i) {
264+
tf::SimpleThreadpool tp(i);
265+
test_dynamic_tasking(tp);
266+
}
267+
}
247268
}
248269

249270
// --------------------------------------------------------

0 commit comments

Comments
 (0)