本文整理汇总了C++中ThreadPool::add_task方法的典型用法代码示例。如果您正苦于以下问题:C++ ThreadPool::add_task方法的具体用法?C++ ThreadPool::add_task怎么用?C++ ThreadPool::add_task使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ThreadPool
的用法示例。
在下文中一共展示了ThreadPool::add_task方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
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;
}
示例2: lock
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);
}
示例3:
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);
}
示例4: main
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();
}