#include#include #include #include using namespace std::experimental; int main() { thread_pool pool(2); ExecutionContext ec = pool.executor(); // using the ExecutionContext with a lambda function that waits for 3 seconds auto task1 = ec.wrap([](){ std::this_thread::sleep_for(std::chrono::seconds(3)); std::cout << "Task 1 completed" << std::endl; }); // submit the task to the pool pool.submit(std::move(task1)); // ...other code return 0; }
#includeThis example uses the ThreadPoolExecutor class from the Folly library to create a thread pool with four threads. An ExecutionContext object is then created with the thread pool, which is used to wrap a lambda function that prints a message. The task is then submitted to the pool using the add() method. Package/Library: In example 1, the experimental library thread_pool is used, while in example 2, the Folly library is used.#include #include using namespace folly; int main() { // create a new thread pool with 4 threads auto pool = std::make_shared (4); // create an execution context with the thread pool auto ec = ExecutionContext::create(pool); // using ExecutionContext with a lambda function auto task1 = ec->wrap([](){ std::cout << "Task 1 completed" << std::endl; }); // submit the task to the pool pool->add(std::move(task1)); // ...other code return 0; }