C++ thread 学习笔记
1. 了解 C++ 中的 std::thread 类
- 作用:
std::thread
是 C++ 标准库中用于创建和管理线程的类,可以用于执行并发任务。 - 优势:提供了一种简单而灵活的方式来实现多线程编程,可以充分利用多核处理器的性能优势。
- 发展历程:C++11 引入了
std::thread
类,用于取代旧版的线程管理方式,如 POSIX 线程库等。
示例代码:
#include <iostream>
#include <thread>
void thread_function() {
std::cout << "Hello from thread!" << std::endl;
}
int main() {
std::thread t(thread_function); // 创建线程并指定执行函数
t.join(); // 等待线程执行完毕
return 0;
}
2. 学习 C++ 中的 std::thread 类的多种应用场景
- 并行计算:利用多个线程同时进行计算任务,加速程序执行速度。
- 异步处理:在后台线程中执行耗时的 I/O 操作,避免阻塞主线程。
- 任务分发:将大型任务分解成多个子任务,在多个线程中并行执行,提高效率。
示例代码:
#include <iostream>
#include <thread>
void compute(int start, int end) {
for (int i = start; i <= end; ++i) {
// 进行计算操作
}
}
int main() {
std::thread t1(compute, 1, 1000);
std::thread t2(compute, 1001, 2000);
t1.join();
t2.join();
return 0;
}
3. 掌握 C++ 中的 std::thread 类的使用技巧
- 线程管理:使用
std::thread
对象管理线程的创建、启动、等待和销毁等操作。 - 线程参数传递:可以向线程函数传递参数,实现线程间的数据传递和通信。
- 线程同步:使用互斥锁、条件变量等同步机制确保多个线程之间的数据安全性和一致性。
示例代码:
#include <iostream>
#include <thread>
#include <mutex>
std::mutex mtx; // 互斥锁
void print_thread_id(int id) {
mtx.lock(); // 加锁
std::cout << "Thread ID: " << id << std::endl;
mtx.unlock(); // 解锁
}
int main() {
std::thread t1(print_thread_id, 1);
std::thread t2(print_thread_id, 2);
t1.join();
t2.join();
return 0;
}
4. 实战案例分析
- 多线程排序:使用多个线程同时对大型数据集进行排序,提高排序效率。
#include <iostream>
#include <vector>
#include <thread>
#include <algorithm>
// 排序函数
void parallel_sort(std::vector<int>& data, size_t start, size_t end) {
std::sort(data.begin() + start, data.begin() + end);
}
int main() {
std::vector<int> data = {5, 3, 8, 1, 2, 7, 6, 4};
size_t num_threads = std::thread::hardware_concurrency(); // 获取可用的线程数量
std::vector<std::thread> threads;
size_t chunk_size = data.size() / num_threads;
// 创建并启动线程
for (size_t i = 0; i < num_threads; ++i) {
size_t start = i * chunk_size;
size_t end = (i == num_threads - 1) ? data.size() : (i + 1) * chunk_size;
threads.emplace_back(parallel_sort, std::ref(data), start, end);
}
// 等待线程执行完毕
for (auto& t : threads) {
t.join();
}
// 归并排序结果
std::sort(data.begin(), data.end());
// 输出排序结果
for (const auto& num : data) {
std::cout << num << " ";
}
std::cout << std::endl;
return 0;
}
- 图像处理:将图像处理任务分解成多个子任务,在多线程中并行处理每个子任务,加速图像处理过程。
#include <iostream>
#include <vector>
#include <thread>
// 模拟图像处理函数
void process_image_chunk(std::vector<int>& image_data, size_t start, size_t end) {
for (size_t i = start; i < end; ++i) {
// 对图像数据进行处理
image_data[i] *= 2;
}
}
int main() {
std::vector<int> image_data(1000, 1); // 模拟图像数据
size_t num_threads = std::thread::hardware_concurrency(); // 获取可用的线程数量
size_t chunk_size = image_data.size() / num_threads;
std::vector<std::thread> threads;
// 创建并启动线程
for (size_t i = 0; i < num_threads; ++i) {
size_t start = i * chunk_size;
size_t end = (i == num_threads - 1) ? image_data.size() : (i + 1) * chunk_size;
threads.emplace_back(process_image_chunk, std::ref(image_data), start, end);
}
// 等待线程执行完毕
for (auto& t : threads) {
t.join();
}
// 输出处理后的图像数据(示例)
for (const auto& pixel : image_data) {
std::cout << pixel << " ";
}
std::cout << std::endl;
return 0;
}
- 网络服务器:使用多线程处理客户端请求,提高服务器的并发处理能力。(示例代码略)
通过学习和掌握 C++ 中的 std::thread
类,可以更好地利用多核处理器的性能优势,实现高效的多线程并发编程。