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; } }
Tak for dine kommentarer!
Spørg AI
Spørg AI
Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat
Can you explain the difference between a thread pool and std::async in more detail?
What are some common use cases for thread pools in C++?
How does std::future work with std::async to retrieve results?
Awesome!
Completion rate improved to 7.69
Thread Pools and std::async
Stryg for at vise menuen
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; } }
Tak for dine kommentarer!