C++ 多线程

文章目录

学习C++提供的线程库的原因

  1. Linux 提供的 pthread_create等线程相关函数无法在windows上使用, C++提供的线程相关函数可以跨平台使用
  2. C++使用了面向对象的思想进行了封装,相关线程方法更容易使用 。

线程相关的类

cpp 复制代码
// 调用常用方法的类
// thread对象、mutex对象均不允许拷贝,但允许移动赋值/构造。
std::thread
std::mutex
std:condition_variable

线程初始化

线程初始化 :

cpp 复制代码
// 线程初始
// 使用函数指针初始化线程
// threadFunction 为启动的线程函数
// x1, x2 为更具threadFunction形参传入的参数,如果threadFunction没参数就不传入
std::thread t1(threadFunction, x1, x2); 

线程批量初始化:

方案一:

cpp 复制代码
	const int numThreads = 5;
    std::thread threads[numThreads]; // 创建多个线程对象
    // 启动多个线程
    for (int i = 0; i < numThreads; ++i) 
    {   // 启动线程,执行 threadFunction(i) // 一定为移动赋值
        threads[i] = std::thread(threadFunction, i); 
    }
    // 等待所有线程执行完毕 
    for (int i = 0; i < numThreads; ++i) 
    {
    	// 线程回收
        threads[i].join();
    }

方案二

cpp 复制代码
	vector<std::thread> treadPool//创建
	for (int i = 0; i < threadPool.size(); ++i) 
    {   
    	// 一定为移动赋值  //启动线程
    	thradPool[i] = std::thread(threadFunction, i); 
    }
    // 等待所有线程执行完毕 
    for (int i = 0; i < numThreads; ++i) 
    {
        threads[i].join();
    }

线程获取自身线程ID

cpp 复制代码
#include <iostream>
#include <thread>

void threadFunction() 
{
	// 注意std::thread::id为线程库中的类型
    std::thread::id this_id = std::this_thread::get_id();
    std::cout << "Thread ID: " << this_id << std::endl;
}

int main() 
{
    std::thread t(threadFunction);
    t.join();
    return 0;
}

线程让出自身时间片

cpp 复制代码
void threadFunction() 
{
    for (int i = 0; i < 5; ++i) 
    {
        std::cout << "Thread A executing..." << std::endl;
        std::this_thread::yield(); // 让出当前线程的执行
    }
}

int main()
{
    std::thread t(threadFunction);
    for (int i = 0; i < 5; ++i) 
    {
        std::cout << "Main thread executing..." << std::endl;
        std::this_thread::yield(); // 让出当前线程的执行
    }
    t.join();
    return 0;
}

互斥锁的基本使用

cpp 复制代码
cpp
#include <iostream>
#include <thread>
#include <mutex>
// 创建一个互斥锁对象
std::mutex mtx; 
void threadFunction(){
    // 在需要保护共享资源的代码块前后分别加锁和解锁
    mtx.lock();
    std::cout << "Thread A executing..." << std::endl;
    mtx.unlock();
}
int main() 
{
    std::thread t(threadFunction);
    // 在需要保护共享资源的代码块前后分别加锁和解锁
    mtx.lock();
    std::cout << "Main thread executing..." << std::endl;
    mtx.unlock();
    t.join();
    return 0;
}

条件变量的基本使用

cpp 复制代码
#include <iostream>
#include <thread>
// 条件变量是通过锁来实现的
#include <mutex>
#include <condition_variable>

std::mutex mtx;
std::condition_variable cv;
bool ready = false;

void threadFunction() 
{
    std::unique_lock<std::mutex> lock(mtx);
    // 等待条件满足
    cv.wait(lock, []{ return ready; });
    std::cout << "Thread A executing..." << std::endl;
}

int main() 
{
    std::thread t(threadFunction);
    {
    	// 出作用域自动解锁 
        std::lock_guard<std::mutex> lock(mtx);
        // 设置条件为true
        ready = true;
    }
    // read已经被修改为true 通知等待的线程可以开始执行
    cv.notify_one();
    t.join();
    return 0;
}
相关推荐
山登绝顶我为峰 3(^v^)339 分钟前
如何录制带备注的演示文稿(LaTex Beamer + Pympress)
c++·线性代数·算法·计算机·密码学·音视频·latex
Python×CATIA工业智造3 小时前
Frida RPC高级应用:动态模拟执行Android so文件实战指南
开发语言·python·pycharm
十五年专注C++开发3 小时前
CMake基础:条件判断详解
c++·跨平台·cmake·自动化编译
我叫小白菜4 小时前
【Java_EE】单例模式、阻塞队列、线程池、定时器
java·开发语言
狐凄4 小时前
Python实例题:基于 Python 的简单聊天机器人
开发语言·python
weixin_446122465 小时前
JAVA内存区域划分
java·开发语言·redis
悦悦子a啊5 小时前
Python之--基本知识
开发语言·前端·python
QuantumStack6 小时前
【C++ 真题】P1104 生日
开发语言·c++·算法
天若有情6736 小时前
01_软件卓越之道:功能性与需求满足
c++·软件工程·软件
whoarethenext6 小时前
使用 C++/OpenCV 和 MFCC 构建双重认证智能门禁系统
开发语言·c++·opencv·mfcc