Thread Pools and std::async
Thread Pool is a collection of pre-created threads that can execute multiple tasks efficiently without repeatedly creating and destroying threads. It improves performance by reusing threads and managing system resources effectively.
Thread pools are a foundational pattern in concurrent programming. Instead of spawning a new thread for each task—which is costly—a thread pool keeps a fixed number of threads alive and reuses them to handle incoming work. This approach reduces overhead, improves CPU utilization, and prevents excessive thread creation.
C++ simplifies asynchronous programming with std::async, which automatically handles task execution. Depending on the launch policy, it may run a task in a new thread or defer it until needed. While not a true thread pool, std::async provides a simple and effective way to run multiple tasks concurrently and collect their results using std::future.
main.cpp
123456789101112131415161718192021222324252627#include <iostream> #include <future> #include <vector> #include <chrono> // Simulate a time-consuming computation int compute_square(int x) { std::this_thread::sleep_for(std::chrono::milliseconds(100)); return x * x; } int main() { std::vector<std::future<int>> futures; // Launch several asynchronous tasks for (int i = 1; i <= 5; ++i) futures.push_back(std::async(std::launch::async, compute_square, i)); // Retrieve and print results for (int i = 0; i < futures.size(); ++i) { int result = futures[i].get(); std::cout << "Square of " << (i + 1) << " is " << result << std::endl; } }
Дякуємо за ваш відгук!
Запитати АІ
Запитати АІ
Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат
Awesome!
Completion rate improved to 7.69
Thread Pools and std::async
Свайпніть щоб показати меню
Thread Pool is a collection of pre-created threads that can execute multiple tasks efficiently without repeatedly creating and destroying threads. It improves performance by reusing threads and managing system resources effectively.
Thread pools are a foundational pattern in concurrent programming. Instead of spawning a new thread for each task—which is costly—a thread pool keeps a fixed number of threads alive and reuses them to handle incoming work. This approach reduces overhead, improves CPU utilization, and prevents excessive thread creation.
C++ simplifies asynchronous programming with std::async, which automatically handles task execution. Depending on the launch policy, it may run a task in a new thread or defer it until needed. While not a true thread pool, std::async provides a simple and effective way to run multiple tasks concurrently and collect their results using std::future.
main.cpp
123456789101112131415161718192021222324252627#include <iostream> #include <future> #include <vector> #include <chrono> // Simulate a time-consuming computation int compute_square(int x) { std::this_thread::sleep_for(std::chrono::milliseconds(100)); return x * x; } int main() { std::vector<std::future<int>> futures; // Launch several asynchronous tasks for (int i = 1; i <= 5; ++i) futures.push_back(std::async(std::launch::async, compute_square, i)); // Retrieve and print results for (int i = 0; i < futures.size(); ++i) { int result = futures[i].get(); std::cout << "Square of " << (i + 1) << " is " << result << std::endl; } }
Дякуємо за ваш відгук!