int main() { ThreadPool tp; tp.add_task( []() { std::cout << "Task1" << std::endl;} ); tp.add_task( []() { std::cout << "Task2" << std::endl;} ); tp.add_task( []() { std::cout << "Task3" << std::endl;} ); tp.add_task( []() { newTask(1); } ); tp.add_task( []() { newTask(2); } ); tp.add_task( []() { newTask(3); } ); std::future<int> res = tp.add_future_task<int>(Question); std::cout << res.get() << std::endl; }
TEST(ThreadPoolTest, TestJoinInDestructor) { int execute_count = 0; std::mutex mutex; { ThreadPool pool; for (int i = 0; i < 5; i++) { // Use lambda to demostrate pool.add_task([&mutex, &execute_count]() -> Status { std::unique_lock<std::mutex> lock(mutex); execute_count++; return Status::OK; }); } } EXPECT_EQ(5, execute_count); }
TEST(ThreadPoolTest, TestTaskObjects) { ThreadPool pool; int execute_count = 0; std::mutex mutex; std::vector<TestTask> tasks; std::vector<ThreadPool::FutureType> results; for (int i = 0; i < 100; i++) { tasks.emplace_back(&execute_count, &mutex); results.emplace_back(pool.add_task(std::bind(tasks.back()))); } pool.close(); pool.join(); for (auto& rst : results) { EXPECT_TRUE(rst.get().ok()); } EXPECT_EQ(100, execute_count); }
int main(int argc, char* argv[]) { ifstream fin(argv[1]); string ip, port; fin >> ip >> port; cout << ip << " " << port << endl;; fin.close(); ThreadPool pool; Socket server(ip, port); pool.start_pool(); int len = 1024*1024; char *buf = new char[len]; while(true) { Task task; server.recv_msg(buf, len); task.client = server.get_client(); task.request = buf; pool.add_task(task); } pool.stop_pool(); }