Multi-threaded programming improves efficiency by executing tasks in parallel. The steps to implement multi-threading using the C++ standard thread library are as follows: Use std::thread to create a thread and pass in a callable object (lambda function or function pointer) as a constructor parameter. Wait for the thread to terminate by calling the join() method, blocking the main thread until the child thread completes execution. Practical case: Calculate prime numbers in parallel, allocate the calculation range to multiple threads and wait for its completion, and print the list of prime numbers after merging the results.
Use the C++ standard thread library to implement multi-threaded programming
Multi-threaded programming improves program efficiency by executing multiple tasks in parallel. This article will introduce how to use the C++ standard thread library to easily implement multi-threaded programming, and illustrate it through practical cases.
Create a thread
To create a thread, you can use the std::thread
class, which receives a reference to a callable object as its constructor parameters. A callable object is usually a lambda function or a function pointer. For example:
// 定义一个函数指针 void thread_function() { // ... 执行此线程应执行的任务 } // 创建线程 std::thread t(thread_function);
Waiting for thread termination
After creating a thread, the main thread can wait for its termination by calling the join()
method. join()
method will block the main thread until the created thread, called the child thread, completes its execution. For example:
// 等待子线程完成 t.join();
Practical case: Parallel calculation of prime numbers
Let us demonstrate multi-threaded programming through a practical case. We will write a program that uses multiple threads to calculate prime numbers in a given range in parallel.
#include <iostream> #include <vector> #include <thread> bool is_prime(int n) { if (n < 2) { return false; } for (int i = 2; i <= n / 2; ++i) { if (n % i == 0) { return false; } } return true; } std::vector<int> find_primes(int start, int end) { std::vector<int> primes; for (int i = start; i <= end; ++i) { if (is_prime(i)) { primes.push_back(i); } } return primes; } int main() { int start = 1; int end = 1000000; int num_threads = 4; // 分配计算范围 int range_size = (end - start) / num_threads; std::vector<std::thread> threads; std::vector<std::vector<int>> primes_list; for (int i = 0; i < num_threads; ++i) { int thread_start = start + i * range_size; int thread_end = thread_start + range_size - 1; threads.emplace_back(std::thread(find_primes, thread_start, thread_end)); } // 等待线程完成并合并结果 for (auto& thread : threads) { std::vector<int> primes; thread.join(); thread.get(primes); primes_list.push_back(primes); } std::vector<int> primes; for (auto& list : primes_list) { primes.insert(primes.end(), list.begin(), list.end()); } // 打印素数列表 for (int prime : primes) { std::cout << prime << " "; } std::cout << std::endl; return 0; }
Running this program will output all prime numbers in the given range.
Note: This tutorial demonstrates the basic concepts and practical applications of multi-threaded programming using the C++ standard thread library. For more advanced features and best practices, it is recommended to consult the C++ standard library documentation and online resources.
The above is the detailed content of How to implement multi-threaded programming using the C++ standard thread library?. For more information, please follow other related articles on the PHP Chinese website!