国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C++ > 正文

C++ 11 三個線程打印ABC(順序打印)

2019-11-06 07:22:04
字體:
來源:轉載
供稿:網友

題目:有3個線程A,B, C, 請用多線程編程實現在屏幕上循環打印10次ABCABC..., 其中A線程打印“A”, B線程打印“B”, C線程打印“C”。

使用C++11 實做, 代碼如下:

#include <iostream>#include <thread>#include <condition_variable>#include <vector>#include <algorithm>std::mutex mtx;std::condition_variable cvar;char g_ch = 0;void PRint_fun(char ch){	int cyle_cnt = 10;	char ch_ = ch - 'A';	for (int i = 0; i < cyle_cnt; i++)	{		std::unique_lock<std::mutex>ulk(mtx);		cvar.wait(ulk, [ch_] {return ch_ == g_ch; });		std::cout << (char)(ch_ + 'A');		g_ch = (ch_ + 1) % 3;		ulk.unlock();		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'));	std::for_each(threads.begin(), threads.end(), std::mem_fn(&std::thread::join));	std::cout << std::endl;	system("pause");	return 0;}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 莆田市| 渝中区| 临邑县| 元氏县| 金阳县| 乌拉特前旗| 宿松县| 友谊县| 山阴县| 凌云县| 天祝| 宜阳县| 安宁市| 子长县| 临江市| 平阳县| 文成县| 皋兰县| 乐安县| 西乌珠穆沁旗| 兰西县| 仙游县| 西林县| 兴山县| 盐山县| 平潭县| 松原市| 葫芦岛市| 郯城县| 平南县| 金川县| 北流市| 阿拉善左旗| 怀远县| 巴林左旗| 岑巩县| 昭平县| 达尔| 丰镇市| 永定县| 乐安县|