c++20 多线程并发 latch & barrier & semaphore

背景:

c++20 关于多线程并发新增了 latch, barrier, semaphore ,接下来就按照一个一个进行介绍

latch

latch 是一次性使用的线程协调点, 一旦给定数量的线程达到latch点时, 所有线程都会解除阻塞, 并继续执行. 基本上它是一个计数器, 在每个线程到达latch点时倒数, 一旦计数器达到0, latch 将无限制保持在一个有信号的状态, 所有阻塞线程都将解除阻塞, 随后到达latch点的任何线程会立刻被允许执行.

latch 由 std::latch实现, 在<latch> 中定义, 构造函数接收需要到达latch点的所需线程数, 到达latch点的线程可以调用arrive_and_wait(), 它递减latch 计数器并阻塞, 直到latch有信号为止. 线程也可以通过调用wait 在不减少计数器的情况下阻塞在latch点上, try_wait() 方法可用与检查计数器是否达到零, 最后如果需要,还可以通过count_down()来减少计数器, 而不会阻塞.

代码如下:

复制代码
#include<latch>
#include <vector>
#include <iostream>
#include <thread>
#include <memory>
#include <chrono>

int main()
{
	std::vector<std::jthread> threads;
	std::latch startLatch{ 1 };
	for (int i = 0; i < 10; ++i)
	{
		threads.push_back(std::jthread{[i, &startLatch]{
			std::cout<< i <<" in wait" <<std::endl;
			startLatch.wait();
			std::cout<< i <<" runing" <<std::endl;
		}});
	}
	std::cout<<"start before"<<std::endl;
	std::this_thread::sleep_for(std::chrono::milliseconds(5));
	startLatch.count_down();
	std::cout<<"start after"<<std::endl;
	return 0;
}

clang++ -std=c++20 -pthread latch.cpp

barrier

barrier 是由一系列阶段组成的可重用线程协调机制, 允许多线程在barrier点阻塞. 当给定数量的线程到达barrier时, 将执行完成阶段的回调, 解除所有阻塞线程的阻塞, 重制线程技术器, 并开始下一个阶段. 在每隔阶段中, 可以调整下一个阶段的预期线程数. barrier对于在循环之间执行同步非常有用. 例如假设多线程并发执行, 并在一个循环中执行一些计算. 进一步假设一旦这些计算执行完成, 需要在线程开始其循环的新迭代之前对结果进一步处理. 对于这种情况, 设置barrier是完美的, 所有的线程都会阻塞在barrier处, 当它们全部到达时, 完成阶段回调将处理结果, 然后解除所有阻塞, 以开始它们的下一次迭代.

barrier 由 std::barrier实现, 在<barrier>中定义, barrier 最重要的方式是arrive_and_wait(), 他减少计数器, 然后阻塞线程, 直到当前阶段完成.

下边是具体代码示例

复制代码
#include<thread>
#include<iostream>
#include<vector>
#include <chrono>
#include <barrier>
void completionFunction() noexcept 
{
}

int main()
{
	const size_t numberOfThreads{4};
	std::barrier barrierPoint {numberOfThreads, completionFunction};
	std::vector<std::jthread> threads;
    
	for (int i = 0; i < numberOfThreads; ++i)
	{
		threads.push_back(std::jthread{[i,&barrierPoint](std::stop_token token){
		while (!token.stop_requested())
		{
			std::cout<<i<<" in wait"<<std::endl;
			barrierPoint.arrive_and_wait();
			std::cout<<i<<" runing"<<std::endl;
            std::this_thread::sleep_for(std::chrono::milliseconds(1000));
		}}});
	}
    return 0;
}

clang++ -std=c++20 -pthread barrier.cpp

semphore

semphore 信号量 是轻量级同步原语, 可用作其他同步机制(如mutex, latch, barrier)的构建块. 基本上上一个semaphore由一个表示很多插槽的计数器组成. 计数器在构造函数中初始化. 如果获得了一个插槽, 计数器就减少, 而释放插槽将增加计数器. 在<semaphore>中定义了两个semaphore类, std::count_semaphore 和 binary_semaphore. 前一种模型是非负资源计数, 后者只有一个插槽, 该槽事空的, 要么不是空的, 完全适合作为互斥的构建快.

代码如下:

复制代码
#include<iostream>
#include <semaphore>
#include <thread>
#include <vector>
int main()
{
	std::counting_semaphore semaphore{4};
	std::vector<std::jthread> threads;
	for (int i = 0; i < 4; i++)
	{
		threads.push_back(std::jthread{[&semaphore](){
			semaphore.acquire();
			semaphore.release();
		}});
	}	
}

clang++ -std=c++20 -pthread barrier.cpp

相关推荐
睡美人的小仙女1274 小时前
Threejs加载环境贴图报错Bad File Format: bad initial token
开发语言·javascript·redis
rayufo4 小时前
【工具】列出指定文件夹下所有的目录和文件
开发语言·前端·python
RANCE_atttackkk4 小时前
[Java]实现使用邮箱找回密码的功能
java·开发语言·前端·spring boot·intellij-idea·idea
缺点内向5 小时前
C#编程实战:如何为Word文档添加背景色或背景图片
开发语言·c#·自动化·word·.net
一起养小猫5 小时前
Flutter for OpenHarmony 实战:记账应用数据统计与可视化
开发语言·jvm·数据库·flutter·信息可视化·harmonyos
zhougl9965 小时前
Java 所有关键字及规范分类
java·开发语言
java1234_小锋5 小时前
Java高频面试题:MyISAM索引与InnoDB索引的区别?
java·开发语言
2501_944525546 小时前
Flutter for OpenHarmony 个人理财管理App实战 - 支出分析页面
android·开发语言·前端·javascript·flutter
Bella的成长园地6 小时前
面试中关于 c++ async 的高频面试问题有哪些?
c++·面试
彷徨而立6 小时前
【C/C++】什么是 运行时库?运行时库 /MT 和 /MD 的区别?
c语言·c++