Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Thread Pools and std::async | Advanced Concurrency Techniques
Quizzes & Challenges
Quizzes
Challenges
/
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

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 3. Розділ 1

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

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

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 3. Розділ 1
some-alt