C++多线程常用方法

[复制链接]
发表于 2025-12-5 06:24:46 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
在 C++ 中,线程干系功能告急通过头文件提供的类和函数来实现,以下是一些常用的线程接口方法和利用本领:
std::thread类

构造函数:
可以通过传入可调用对象(如函数指针、函数对象、lambda 表达式等)来创建一个线程实例,启动一个新线程去实行对应的任务。比方:
  1. #include <iostream>
  2. #include <thread>
  3. void hello() {
  4.     std::cout << "Hello from thread!" << std::endl;
  5. }
  6. int main() {
  7.     std::thread t(hello);  // 创建线程t并开始执行hello函数
  8.     t.join();  // 等待线程t执行完毕
  9.     return 0;
  10. }
复制代码
这里std::thread t(hello)就是利用函数指针hello创建线程,新线程会实行hello函数里的代码。
join方法:
用于壅闭当火线程,直到被调用的线程实行完成。比如在上面的main函数中,t.join()会让main线程停息,等候t线程把hello函数实行完后再继续往下实行。
detach方法:
将线程分离,使得线程在配景独立运行,不再与创建它的std::thread对象关联。今后,无法再通过该std::thread对象对这个线程举行控制(比如不能再调用join了)。示例:
  1. #include <iostream>
  2. #include <thread>
  3. void func() {
  4.     // 线程执行的函数内容
  5.     std::cout << "Thread is running independently." << std::endl;
  6. }
  7. int main() {
  8.     std::thread t(func);
  9.     t.detach();  // 分离线程t
  10.     // 主线程继续执行其他操作,不用等待t线程结束
  11.     std::cout << "Main thread continues." << std::endl;
  12.     return 0;
  13. }
复制代码
线程传参
向线程函数通报参数时,必要包管参数在通报时是有效的,且在被调用函数实行期间一连有效(比如克制传引用指向临时对象等情况)。比方:
  1. #include <iostream>
  2. #include <thread>
  3. void print_num(int num) {
  4.     std::cout << "The number is: " << num << std::endl;
  5. }
  6. int main() {
  7.     int num = 10;
  8.     std::thread t(print_num, num);  // 传递普通变量num作为参数
  9.     t.join();
  10.     return 0;
  11. }
复制代码
如果要通报类对象等更复杂的情况,要注意拷贝、移动语义等干系题目,确保参数通报的准确性。
线程 ID 获取
可以通过std::this_thread::get_id获取当火线程的线程 ID,大概通过std::thread对象的get_id成员函数获取对应的线程 ID,用于标识和区分差别线程。示例:
  1. #include <iostream>
  2. #include <thread>
  3. void show_thread_id() {
  4.     std::cout << "Thread ID: " << std::this_thread::get_id() << std::endl;
  5. }
  6. int main() {
  7.     std::thread t(show_thread_id);
  8.     std::cout << "Main thread ID: " << std::this_thread::get_id() << std::endl;
  9.     t.join();
  10.     return 0;
  11. }
复制代码
线程同步干系(比方互斥量等,用于办理多线程访问共享资源辩论题目)

std::mutex(互斥量):
通过lock和unlock方法对共享资源举行加锁息争锁,确保同一时候只有一个线程能访问被掩护的共享资源。比方:
  1. #include <iostream>
  2. #include <thread>
  3. #include <mutex>
  4. std::mutex mtx;
  5. int shared_data = 0;
  6. void increment() {
  7.     for (int i = 0; i < 1000; ++i) {
  8.         mtx.lock();  // 加锁
  9.         shared_data++;
  10.         mtx.unlock();  // 解锁
  11.     }
  12. }
  13. int main() {
  14.     std::thread t1(increment);
  15.     std::thread t2(increment);
  16.     t1.join();
  17.     t2.join();
  18.     std::cout << "Shared data value: " << shared_data << std::endl;
  19.     return 0;
  20. }
复制代码
也可以利用std::lock_guard等 RAII(Resource Acquisition Is Initialization)机制的类来更方便、安全地管理互斥量的生命周期,主动完成加锁息争锁,如:
  1. #include <iostream>
  2. #include <thread>
  3. #include <mutex>
  4. std::mutex mtx;
  5. int shared_data = 0;
  6. void increment() {
  7.     for (int i = 0; i < 1000; ++i) {
  8.         std::lock_guard<std::mutex> guard(mtx);  // 构造时加锁,析构时自动解锁
  9.         shared_data++;
  10.     }
  11. }
  12. int main() {
  13.     std::thread t1(increment);
  14.     std::thread t2(increment);
  15.     t1.join();
  16.     t2.join();
  17.     std::cout << "Shared data value: " << shared_data << std::endl;
  18.     return 0;
  19. }
复制代码
std::condition_variable(条件变量):
常和互斥量共同利用,用于线程间的同步,实现一个线程等候某个条件满足后再继续实行的功能。比如一个线程等候另一个线程修改共享资源到达某个条件后再举行后续利用,典范用法如下:
  1. #include <iostream>
  2. #include <thread>
  3. #include <mutex>
  4. #include <condition_variable>
  5. std::mutex mtx;
  6. std::condition_variable cv;
  7. bool ready = false;
  8. void wait_for_signal() {
  9.     std::unique_lock<std::mutex> lck(mtx);
  10.     cv.wait(lck, []{ return ready; });  // 等待条件满足(ready为true)
  11.     std::cout << "Received signal and continue." << std::endl;
  12. }
  13. void send_signal() {
  14.     {
  15.         std::lock_guard<std::mutex> lck(mtx);
  16.         ready = true;
  17.     }
  18.     cv.notify_one();  // 通知等待在该条件变量上的一个线程
  19. }
  20. int main() {
  21.     std::thread t1(wait_for_signal);
  22.     std::thread t2(send_signal);
  23.     t1.join();
  24.     t2.join();
  25.     return 0;
  26. }
复制代码
这些就是 C++ 中线程干系的一些告急接口方法及其根本利用方式,在实际多线程编程中,通常必要综合运用它们来实现高效、准确的并发步伐逻辑。
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表