多线程顺序打印以及生产消费者

三个线程顺序打印A,B,C

#include<iostream>
#include<thread>
#include<mutex>
#include <condition_variable> 
using namespace std;
std::mutex mtx;
std::condition_variable cv;
int num = 1;
const int n = 99;
int i = 0;

void print_A()
{
	std::unique_lock<std::mutex> lock(mtx);  // std::lock_guard<std::mutex> lock(mtx);
	while (i <= n)
	{
		while (num != 1)
		{
			cv.wait(lock);        
			 //1.释放锁 2.挂起,等待返回 3.被唤醒后需要重新持有锁才能返回
		}
		if (i <= n)
		{
			cout << "A:" << i << endl;
			num = 2;
			++i;
			std::this_thread::sleep_for(std::chrono::microseconds(1000));
		}
		cv.notify_all();             //唤醒所有挂起的线程
	}
}

void print_B()
{
	std::unique_lock<std::mutex> lock(mtx);
	while (i < n)
	{
		while (num != 2)
		{
			cv.wait(lock);
		}
		if (i <= n)
		{
			cout << "B" << i << endl;
			num = 3;
			++i;
			std::this_thread::sleep_for(std::chrono::microseconds(1000));
		}
		cv.notify_all();
	}
}

void print_C()
{
	std::unique_lock<std::mutex> lock(mtx);
	while (i < n)
	{
		while (num != 3)
		{
			cv.wait(lock);
		}
		if (i <= n)
		{
			cout << "C" << i << endl;
			num = 1;
			++i;
			std::this_thread::sleep_for(std::chrono::microseconds(1000));
		}
		cv.notify_all();
	}
}

int main()
{
	thread t1(print_A);
	thread t2(print_B);
	thread t3(print_C);
	t1.join();
	t2.join();
	t3.join();
    system("pause");
	return 0;
}

condition_variable用来唤醒一个或多个等待在某特定条件上的线程,所以 condition_variable 通常在一个 while 循环里面。

顺序打印ABC,三个线程调用一个函数

#include <iostream>
#include <thread>
#include <condition_variable>
#include <vector>
#include <algorithm>

char g_ch = 0;
 
void print_fun(char ch)
{
	int cyle_cnt = 10;
 
	for (int i = 0; i < cyle_cnt; i++)
	{
		{
                std::unique_lock<std::mutex>ulk(mtx);
		cvar.wait(ulk, [&] {return ch - 'A' == g_ch; }); // 判断条件为true 那么就不阻塞。
                }
		std::cout << (char)(ch);
		g_ch = (ch - 'A' + 1) % 3
		cvar.notify_all();
	}
}
 
int main()
{
	std::vector<std::thread> threads;
	threads.push_back(std::thread(print_fun, 'A'));
	threads.push_back(std::thread(print_fun, 'B'));
	threads.push_back(std::thread(print_fun, 'C'));
	for(auto& thread : threads){
		thread.join();
	}
	std::cout << std::endl;
	system("pause");
	return 0;
}

多线程模拟生产消费模式

#include <iostream>
#include <thread>
#include <condition_variable>
#include <queue>
#include <algorithm>
 
std::mutex mtx;
std::condition_variable cvar;
std::queue<int> que;

void providers(int val){
	for(int i = 0; i < 6; i++) { // 每一个provider 线程提供提供六次
		{
			std::unique_lock<std::mutex> lck(mtx);
			que.push(val+i);
		}
		cvar.notify_one();
	}
}

void consumers(int num){

	while (true){
		int val;
		{
			std::unique_lock<std::mutex> clck(mtx);
			cvar.wait(clck,[&]{return !que.empty();});
			val = que.front(); que.pop();
		}
		std:: cout << "consumer : " <<num << "  " << val << std::endl;

	}

}

int main(){
	std::thread t1(providers, 100);
	std::thread t2(providers, 200);
	std::thread t3(consumers, 1);
	std::thread t4(consumers, 2);
	t1.join();
	t2.join();
	t3.join();
	t4.join();

	system("pause");
	return 0;
}
原文地址:https://www.cnblogs.com/wsl-hitsz/p/14523749.html