多线程---死锁问题

死锁1:

std::mutex mx;               //互斥量
std::condition_variable cv;  //信号量
int number = 0;

const int n = 3;
void func1()
{
	std::unique_lock<mutex>lock(mx);
	while (number <= n)
	{
		while (number % 3 == 0  && number <=n)
		{
			cout << "fun1: " << number << " " << endl;
			number += 1;
			cv.notify_one();
		}
		cv.wait(lock);
	}
}

void func2()
{
	std::unique_lock<mutex>lock(mx);
	while (number <= n)
	{
		while (number % 3 == 1 && number <= n)
		{
			cout << "fun2: " << number << " " << endl;
			number += 1;
			cv.notify_one();
		}
		cv.wait(lock);
	}
}

void func3()
{
	std::unique_lock<mutex>lock(mx);
	while (number <= n)
	{
		while (number %  3== 2&& number <= n)
		{
			cout << "fun3: " << number << " " << endl;
			number += 1;
			cv.notify_one();
		}
		cv.wait(lock);
	}
}

int main()
{
	thread tha(func1);
	thread thb(func2);
	thread thc(func3);

	tha.join();
	thb.join();
	thc.join();

	return 0;

}

在这里插入图片描述

死锁2:

std::mutex mx;               //互斥量
std::condition_variable cv;  //信号量
int number = 0;

const int n = 3;
void func1()
{
	std::unique_lock<mutex>lock(mx);
	while (number <= n)
	{
		while (number % 3 == 0  && number <=n)
		{
			cout << "fun1: " << number << " " << endl;
			number += 1;
			cv.notify_all();
		}
		cv.wait(lock);
	}
}

void func2()
{
	std::unique_lock<mutex>lock(mx);
	while (number <= n)
	{
		while (number % 3 == 1 && number <= n)
		{
			cout << "fun2: " << number << " " << endl;
			number += 1;
			cv.notify_all();
		}
		cv.wait(lock);
	}
}

void func3()
{
	std::unique_lock<mutex>lock(mx);
	while (number <= n)
	{
		while (number %  3== 2&& number <= n)
		{
			cout << "fun3: " << number << " " << endl;
			number += 1;
			cv.notify_all();
		}
		cv.wait(lock);
	}
}

int main()
{
	thread tha(func1);
	thread thb(func2);
	thread thc(func3);

	tha.join();
	thb.join();
	thc.join();

	return 0;

}

在这里插入图片描述
说明还有进程在等待队列上,无法正常退出

修改正常:程序最终在唤醒一次

std::mutex mx;               //互斥量
std::condition_variable cv;  //信号量
int number = 0;

const int n = 3;
void func1()
{
	std::unique_lock<mutex>lock(mx);
	while (number <= n)
	{
		while (number % 3 == 0  && number <=n)
		{
			cout << "fun1: " << number << " " << endl;
			number += 1;
			cv.notify_one();
		}
		cv.wait(lock);
	}
	cv.notify_one();
}

void func2()
{
	std::unique_lock<mutex>lock(mx);
	while (number <= n)
	{
		while (number % 3 == 1 && number <= n)
		{
			cout << "fun2: " << number << " " << endl;
			number += 1;
			cv.notify_one();
		}
		cv.wait(lock);
	}
	cv.notify_one();
}

void func3()
{
	std::unique_lock<mutex>lock(mx);
	while (number <= n)
	{
		while (number %  3== 2&& number <= n)
		{
			cout << "fun3: " << number << " " << endl;
			number += 1;
			cv.notify_one();
		}
		cv.wait(lock);
	}
	cv.notify_one();
}

int main()
{
	thread tha(func1);
	thread thb(func2);
	thread thc(func3);

	tha.join();
	thb.join();
	thc.join();

	return 0;

}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值