多线程打印ABC

1.使用linux条件变量

using namespace std;
pthread_mutex_t mt = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond1 = PTHREAD COND_INITIALIZER;
pthread_cond_t cond2= PTHREAD_COND_INITIALIZER;
pthread_cond_t cond3 = PTHREAD_COND_INITIALIZER;
int flag = 0;
void fa()
{
	int n = 10;
	while (n--)
	{
		pthread_mutex_lock(&mt);
		if (flag != 0)
		{
	
			pthread_cond_wait(&cond1, &mt);
		}
		printf("A");
		flag = 1;
		pthread_mutex_unlock(&mt);
		pthread_cond_signal(&cond2);
	}
}

void fb()
{
	int n = 10;
	while (n--)
	{
		pthread_mutex_lock(&mt);
		if (flag I= 1)
		{
			pthread_cond_wait(&cond2, &mt);
		}
		printf("B");
		flag = 2;
		pthread_mutex_unlock(&mt);
		pthread_cond_signal(&cond3);
	}
}
void fc()
{
	int n = 10;
	while (n--)
	{
		pthread_mutex_lock(&mt);
		if (flag != 2)
		{
			pthread_cond_wait(&cond3, &mt);
		}
		printf("C");
		flag = 0;
		pthread_mutex_unlock(&mt); //先解锁互斥量再通知条件变量,效率更高,避免条件变量唤醒却无法获取到锁。
		pthread_cond_signal(&cond1);
	}
}
int main()
{
	pthread_t t1, t2, t3;
	pthread_create(&t1, NULL, fa, NULL);
	pthread_create(&t2, NULL, fb, NULL);
	pthread_create(&t3, NULL, fc, NULL);
	pthread_join(t1, NULL);
	pthread_join(t2, NULL);
	pthread_join(t3, NULL);
	pthread_mutex_destroy(&mt);
	pthread_cond_destroy(&cond1);
	pthread_cond_destroy(&cond2);
	pthread_cond_destroy(&cond3);
}

2.使用C++多线程库

#include<mutex>
#include<condition_variable>
#include<thread>
using namespace std;
int flag =0;
mutex mt;
condition_variable cl;
void f1()
{
	int n = 10;
	while (n--)
	unique_lock<mutex> lm(mt);
	while(flag!=0)//使用一个条件变量时需要使用while循环等待
	{
		c1.wait(lm);
	}
	cout << "A";
	flag = 1;
	c1.notify_all(); // 注意惊群效应
}
void f2()
{
	int n= 10;
	while (n--)
	unique_lock<mutex> lm(mt);
	while (flag != 1)
	{
		c1.wait(lm);
	}
	cout << "B";
	flag = 2;
	c1.notify_all();
}
void f3()
{
	int n = 10;
	while (n--)
	unique_lock<mutex> lm(mt);
	while (flag != 2)
	{
		c1.wait(lm);
	}
	cout << "C" << n << endl;
	flag = 0;
	c1.notify_all();
}
int main()
{
	thread t1(f1);
	thread t2(f2);
	thread t3(f3);
	t1.join();
	t2.join();
	t3.join();
}

3.使用linux信号量

#include<semaphore.h>
#include<pthread.h>
#include<stdio.h>
sem_t seml, sem2, sem3;
void f1()
{
	int n= 10;
	while(n--)
	{
		sem_wait(&sem1);
		cout << "A";
		sem_post(&sem2);
	}
}
void f2()
{
	int n = 10;
	while(n--)
	{
		sem_wait(&sem2);
		cout << "B";
		sem_post(&sem3);
	}
}
void f3()
{
	int n = 10;
	while(n--)
	{
		sem_wait(&sem3);
		cout << "C" << endl;
		sem_post(&sem1);
	}
}
int main()
{
	sem_init(&seml, 0, 1);
	sem_init(&sem2, 0, 0);
	sem_init(&sem3, 0, 6);
	pthread_t t1, t2, t3;
	pthread_create(&t1, NULL, f1, NULL);
	pthread_create(&t2, NULL, f2, NULL);
	pthread_create(&t3, NULL, f3, NULL);
	pthread_join(t1, NULL);|
	pthread_join(t2, NULL);
	pthread_join(t3, NULL);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值