c++ 多线程编程
一. 使用函数对象
#include <iostream> #include <thread> class PrintTask { public: void operator()(int count) const { for (int i = 0; i < count; ++i) { std::cout << "Hello from thread (function object)!\n"; } } }; int main() { std::thread t2(PrintTask(), 5); // 创建线程,传递函数对象和参 ;类名称(). PrintTask() t2.join(); // 等待线程完成 return 0; }
- operator(),这是一个 重载函数调用运算符(function call operator) 的写法。在 C++ 中,通过重载 operator(),我们可以让一个类的对象像函数一样被调用。这种对象被称为 函数对象(functor)
- 参数列表可以是任意的(也可以没有参数)。
const表示这个函数不会修改对象的状态
二. 使用函数指针
#include <iostream> #include <thread> void printMessage(int count) { for (int i = 0; i < count; ++i) { std::cout << "Hello from thread (function pointer)!\n"; } } int main() { std::thread t1(printMessage, 5); // 创建线程,传递函数指针和参数; 函数名称 printMessage t1.join(); // 等待线程完成 return 0; }
三.使用Lambda 表达式
Lambda 表达式可以直接内联定义线程执行的代码
#include <iostream> #include <thread> int main() { std::thread t3([](int count) { for (int i = 0; i < count; ++i) { std::cout << "Hello from thread (lambda)!\n"; } }, 5); // 创建线程,传递 Lambda 表达式和参数 t3.join(); // 等待线程完成 return 0; }
四.引用传参数
#include <iostream>
#include <thread>
//引用传参
void test(int& x){
++x;
}
int main() {
int num=0; //赋初值为1
std::thread t3(test,std::ref(num));
t3.join(); // 等待线程完成
std::cout << num <<std::endl;
return 0;
}
五. 最基本的锁
#include <iostream>
#include <thread>
#include <mutex>
std::mutex mtx; //全局互斥量
int share_data=0; //共享资源(被多个线程访问)
void safeFunction(){
for (int i=0;i<10;i++){
mtx.lock();
share_data++;
mtx.unlock();
}
}
int main() {
int num;
std::thread t1(safeFunction);
std::thread t2(safeFunction);
t1.join(); // 等待线程完成
t2.join(); //等待线程完成
std::cout << "final value of share_data: "<<share_data <<std::endl;
return 0;
}
六. 自动加锁解锁
#include <iostream> #include <thread> #include <mutex> std::mutex mtx; //全局互斥量 int share_data=0; //共享资源(被多个线程访问) void safeFunction(){ //自动加锁解锁 std::lock_guard <std::mutex> lk(mtx); share_data++; } int main() { int num; std::thread t1(safeFunction); std::thread t2(safeFunction); t1.join(); // 等待线程完成 t2.join(); //等待线程完成 std::cout << "final value of share_data: "<<share_data <<std::endl; return 0; }
七.自动加锁解锁,也可以手动解锁
#include <iostream>
#include <thread>
#include <mutex>
std::mutex mtx; //全局互斥量
int share_data=0; //共享资源(被多个线程访问)
void safeFunction(){
//自动加锁解锁
std::unique_lock <std::mutex> uk(mtx);
for(int i=0;i<2;i++){
share_data++;
}
uk.unlock(); //可选: 手动解锁
//做一些不需要加锁的动作
std::cout << "你好" <<std::endl;
}
int main() {
int num;
std::thread t1(safeFunction);
std::thread t2(safeFunction);
t1.join(); // 等待线程完成
t2.join(); //等待线程完成
std::cout << "final value of share_data: "<<share_data <<std::endl;
return 0;
}
八. 原子操作
#include <atomic>
#include <thread>
#include <iostream>
//定义原子形变量count ,并初始化为0
std::atomic<int> count(0);
void increment() {
//count 变量的原子加法操作 fetch_add
//这是 内存顺序(memory order) 参数,表示这个操作不需要任何同步或排序约束。
count.fetch_add(1, std::memory_order_relaxed);
}
int main() {
std::thread t1(increment);
std::thread t2(increment);
t1.join();
t2.join();
std::cout << "final value of share_data: "<<count <<std::endl;
return count;
}
九. 线程局部存储
#include <iostream>
#include <thread>
//thread_local关键字实现,避免了对共享资源的争用
thread_local int threadData = 0;
void threadFunction() {
threadData = 42; // 每个线程都有自己的threadData副本
std::cout << "Thread data: " << threadData << std::endl;
}
int main() {
std::thread t1(threadFunction);
std::thread t2(threadFunction);
t1.join();
t2.join();
return 0;
}
posted on 2025-06-28 12:28 running-fly 阅读(5) 评论(0) 收藏 举报
浙公网安备 33010602011771号