当前位置: 首页 > news >正文

【现代C++】线程支持库

在这里插入图片描述

现代C++(C++11及其之后的版本)引入了标准的线程支持库,使得多线程编程变得更加简单和可移植。这个库提供了线程管理、互斥量、条件变量和其他同步原语。

1. std::thread - 基本线程

std::thread允许创建执行特定任务的线程。

#include <iostream>
#include <thread>void helloFunction() {std::cout << "Hello from thread!" << std::endl;
}void basicThread() {std::thread t(helloFunction);t.join();  // 等待线程完成
}

2. 传递参数给线程函数

线程函数可以接受参数,和普通函数一样。

#include <iostream>
#include <thread>void printMessage(const std::string& message) {std::cout << message << std::endl;
}void threadWithArguments() {std::thread t(printMessage, "Hello from thread with argument!");t.join();
}

3. std::mutex - 互斥量

互斥量用于同步对共享资源的访问。

#include <iostream>
#include <thread>
#include <mutex>std::mutex mtx;void printBlocked(const std::string& message) {mtx.lock();std::cout << message << std::endl;mtx.unlock();
}void mutexExample() {std::thread t1(printBlocked, "Thread 1");std::thread t2(printBlocked, "Thread 2");t1.join();t2.join();
}

4. std::lock_guard - 自动管理互斥量

std::lock_guard提供了一种便捷的RAII风格的方式来自动上锁和解锁互斥量。

#include <iostream>
#include <thread>
#include <mutex>std::mutex mtx;void safePrint(const std::string& message) {std::lock_guard<std::mutex> lock(mtx);std::cout << message << std::endl;// 互斥量在lock_guard对象被销毁时自动解锁
}void lockGuardExample() {std::thread t1(safePrint, "Thread 1 with lock_guard");std::thread t2(safePrint, "Thread 2 with lock_guard");t1.join();t2.join();
}

5. std::asyncstd::future - 异步执行

std::async允许异步执行函数,并通过std::future获取结果。

#include <iostream>
#include <future>int compute() {return 42;  // 模拟计算
}void asyncExample() {std::future<int> result = std::async(compute);std::cout << "The answer is " << result.get() << std::endl;
}

6. std::condition_variable - 条件变量

条件变量用于线程间的同步,允许线程在特定条件下等待或通知其他线程。

#include <iostream>
#include <thread>
#include <mutex>
#include <condition_variable>std::mutex mtx;
std::condition_variable cv;
bool ready = false;void printId(int id) {std::unique_lock<std::mutex> lock(mtx);while (!ready) cv.wait(lock);std::cout << "Thread " << id << std::endl;
}void go() {std::unique_lock<std::mutex> lock(mtx);ready = true;cv.notify_all();
}void conditionVariableExample() {std::thread threads[10];for (int i = 0; i < 10; ++i) {threads[i] = std::thread(printId, i);}std::cout << "10 threads ready to race..." << std::endl;go();for (auto& t : threads) {t.join();}
}
http://www.lryc.cn/news/335764.html

相关文章:

  • 游戏引擎架构01__引擎架构图
  • [Java、Android面试]_15_Android为什么使用Binder?
  • Python+Selenium+Unittest 之Unittest3(TestSuite()和TextTestRunner())
  • 3D桌面端可视化引擎HOOPS Visualize如何实现3D应用快速开发?
  • Vue探索之Vue2.x源码分析(二)
  • 人工智能分类算法概述
  • 理解 Golang 变量在内存分配中的规则
  • 《QT实用小工具·二十四》各种数学和数据的坐标演示图
  • 【S32K3 MCAL配置】-3.1-CANFD配置-经典CAN切换CANFD(基于MCAL+FreeRTOS)
  • IEC101、IEC103、IEC104、Modbus报文解析工具
  • node res.end返回json格式数据
  • 产品开发流程
  • Python蓝桥杯赛前总结
  • 20240326-1-KNN面试题
  • 【论文速读】| MASTERKEY:大语言模型聊天机器人的自动化越狱
  • jvm运行情况预估
  • Day105:代码审计-PHP原生开发篇SQL注入数据库监控正则搜索文件定位静态分析
  • Python:如何对FY3D TSHS的数据集进行重投影并输出为TIFF文件以及批量镶嵌插值?
  • CentOS 镜像下载
  • yum和配置yum源
  • jQuery笔记 02
  • 基于Java+SpringBoot+Vue文学名著分享系统(源码+文档+部署+讲解)
  • C/S医学检验LIS实验室信息管理系统源码 医院LIS源码
  • liunx环境变量学习总结
  • 对于Redis,如何根据业务需求配置是否允许远程访问?
  • 深入分析Linux上下文与上下文切换
  • Docker快速上手及常用命令速查
  • 学习笔记:解决拖延
  • 第一个Swift程序
  • Anthropic Claude 3 加入亚马逊云科技 AI“全家桶”