#include #include"thread_pool.h" namespace { static void TestThread(int id) { printf("Thread %d started\n", id); std::this_thread::sleep_for(std::chrono::seconds(1)); printf("Thread %d stopped\n", id); } } // namespace int main(int argc, char **argv) { ThreadPool tp(5); std::condition_variable cv; int completed_count = 0; std::mutex completed_count_mu; puts("Started threads"); static constexpr int kItemCount = 10; for(int i = 0; i < kItemCount; i++) { tp.Add([i, &completed_count, &completed_count_mu, &cv]() { TestThread(i); std::unique_lock l(completed_count_mu); completed_count++; cv.notify_one(); }); } printf("Waiting for %d items to complete\n", kItemCount); { std::unique_lock l(completed_count_mu); cv.wait(l, [&completed_count]() { return completed_count >= kItemCount; }); } puts("Done"); return 0; }