c++并发编程:面试题-线程同步

是否熟悉POSIX多线程编程技术?如熟悉,编写程序完成如下功能:

1)有一int型全局变量g_Flag初始值为0;

2) 在主线称中起动线程1,打印“this is thread1”,并将g_Flag设置为1

3) 在主线称中启动线程2,打印“this is thread2”,并将g_Flag设置为2

4) 线程序1需要在线程2退出后才能退出

5) 主线程在检测到g_Flag从1变为2,或者从2变为1的时候退出


这里采用C++11实现,关于std::future,std::promise实现一次性同步具体请参见下面链接:一篇文章

#include<iostream>  
#include<functional>  
#include<thread>  
#include<future>  
#include<utility>  
#include<stdio.h>  
#include<chrono>  
#include<atomic>  
#include<condition_variable>
//#include<pthread.h>  
using namespace std;
atomic<int> flag(0);//采用原子操作保护g_Flag的读写  
mutex m;
condition_variable cond1;
condition_variable cond2;
condition_variable cond3;

void worker1(int f1){//线程1  
	unique_lock<mutex>lk(m);
	
	printf("this is thread%d\n",f1);
	flag = 1;
	cond3.notify_one();
	while (flag != 2)
		cond1.wait(lk);
	printf("thread1 exit\n");
	cond3.notify_one();
}

void worker2(int f2){//线程1  
	unique_lock<mutex>lk(m);
	while (1 != flag)
		cond3.wait(lk);
	printf("this is thread%d\n", f2);
	printf("thread2 exit\n");
	   flag = 2;
	cond1.notify_all();
}


int main(){
	
	thread one(worker1, 1);
	thread two(worker2, 2);
	 
	one.detach();
	two.detach();
	//pthread_exit(NULL);//主线程到这里退出  

	unique_lock<mutex>lk(m);
		cond3.wait(lk);
	printf("main thread exit\n");
	return 0;
}


  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值