C++11 线程同步接口std::condition_variable和std::future的简单使用

std::condition_variable

条件变量std::condition_variable有wait和notify接口用于线程间的同步。如下图所示,Thread 2阻塞在wait接口,Thread 1通过notify接口通知Thread 2继续执行。

具体参见示例代码:

c++ 复制代码
#include<iostream>
#include<mutex>
#include<thread>
#include<queue>
std::mutex mt;
std::queue<int> data;
std::condition_variable cv;
auto start=std::chrono::high_resolution_clock::now();

void logCurrentTime()
{
	auto end = std::chrono::high_resolution_clock::now();
	auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
	std::cout << elapsed << ":";
}
void prepare_data()
{	
	logCurrentTime();
	std::cout << "this is " << __FUNCTION__ << " thread:" << std::this_thread::get_id() << std::endl;
	for (int i = 0; i < 10; i++)
	{
		data.push(i);
		logCurrentTime();
		std::cout << "data OK:" << i << std::endl;
	}
	//start to notify consume_data thread data is OK!
	cv.notify_one();
}


void consume_data()
{
	logCurrentTime();
	std::cout << "this is: " << __FUNCTION__ << " thread:" << std::this_thread::get_id() << std::endl;
	std::unique_lock<std::mutex> lk(mt);
	//wait first for notification
	cv.wait(lk);  //it must accept a unique_lock parameter to wait

	while (!data.empty())
	{
		logCurrentTime();
		std::cout << "data consumed: " << data.front() << std::endl;
		data.pop();
	}
}


int main()
{
	std::thread t2(consume_data);
	//wait for a while to wait first then prepare data,otherwise stuck on wait
	std::this_thread::sleep_for(std::chrono::milliseconds(10));
	std::thread t1(prepare_data);
	t1.join();
	t2.join();
	return 0;
}

输出结果

分析

主线程中另启两个线程,分别执行consume_data和prepare_data,其中consume_data要先执行,以保证先等待再通知,否则若先通知再等待就死锁了。首先consume_data线程在从wait 处阻塞等待。后prepare_data线程中依次向队列写入0-10,写完之后通过notify_one 通知consume_data线程解除阻塞,依次读取0-10。

std::future

std::future与std::async配合异步执行代码,再通过wait或get接口阻塞当前线程等待结果。如下图所示,Thread 2中future接口的get或wait接口会阻塞当前线程,std::async异步开启的新线程Thread1执行结束后,将结果存于std::future后通知Thread 1获取结果后继续执行.

具体参见如下代码:

c++ 复制代码
#include <iostream>
#include <future>
#include<thread>

int test()
{
	std::cout << "this is " << __FUNCTION__ << " thread:" << std::this_thread::get_id() << std::endl;;
	std::this_thread::sleep_for(std::chrono::microseconds(1000));
	return 10;
}
int main()
{
	std::cout << "this is " <<__FUNCTION__<<" thread:" << std::this_thread::get_id() << std::endl;;
	//this will lanuch on another thread
	std::future<int> result = std::async(test);

	std::cout << "After lanuch a thread: "<< std::this_thread::get_id() << std::endl;

	//block the thread and wait for the result
	std::cout << "result is: " <<result.get()<< std::endl;

	std::cout << "After get result "<< std::endl;

	return 0;
}

输出结果

分析

主程序中调用std::async异步调用test函数,可以看到main函数的线程ID 27428与test函数执行的线程ID 9704并不一样,说明std::async另起了一个新的线程。在test线程中,先sleep 1000ms,所以可以看到"After lanuch a thread:"先输出,说明主线程异步执行,不受子线程影响。而"After get result "最后输出,说明get()方法会阻塞主线程,直到获取结果。

相关推荐
Tairitsu_H8 小时前
[C++] 深入理解红黑树:封装set与map
开发语言·c++·set·map·红黑树·模拟实现
HugoStudio_SWAN8 小时前
洛谷 B4450 / B3867 / B3923 智慧购物、储蓄与做题——从程序到生活
c++·学习·程序人生·算法·生活
会周易的程序员10 小时前
5Draft(五帝)测试报告
服务器·c++·分布式·raft·共识算法·共识·算力服务器
UIU11410 小时前
scanf与cout的误区:探究其内部的机制
c++·学习·c#·scanf
闻缺陷则喜何志丹10 小时前
【栈 运算系统】P3719 [AHOI2017初中组] rexp|普及+
c++·算法··洛谷·运算系统
bnmoel11 小时前
C++ 基础入门篇(一):命名空间,输入&输出,缺省参数,函数重载
c++·函数重载·语法·命名空间·缺省参数
Zixhy11 小时前
多点巡检机器人项目技术总结
linux·c++·机器人·自动驾驶
Pointer Pursuit13 小时前
C++11特性(二)
前端·c++·算法
tudousisi22214 小时前
CSP 第3题训练 Day 5|CSP202509C HTTP 头信息|Blog A
c++
闻缺陷则喜何志丹15 小时前
【动态规划】P10726 [GESP202406 八级] 空间跳跃|普及+
c++·算法·动态规划·洛谷