Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Apprendre Thread Pools and std::async | Advanced Concurrency Techniques
C++ Concurrency and Multithreading

bookThread Pools and std::async

Note
Definition

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

main.cpp

copy
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; } }
question mark

Which of the following is the correct way to launch multiple concurrent tasks using std::async and retrieve their results?

Select the correct answer

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 3. Chapitre 1

Demandez à l'IA

expand

Demandez à l'IA

ChatGPT

Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion

Suggested prompts:

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?

bookThread Pools and std::async

Glissez pour afficher le menu

Note
Definition

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

main.cpp

copy
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; } }
question mark

Which of the following is the correct way to launch multiple concurrent tasks using std::async and retrieve their results?

Select the correct answer

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 3. Chapitre 1
some-alt