thread数组

  • Post author:
  • Post category:其他



  • 线程数组的创建和使用
	thread n[6];
	for(int i=0 ; i<ret.size() ; i++)
		 n[i] = thread(show, ret[i]);

  • demo
#include <iostream>
#include <thread>
#include <windows.h>
#include <mutex>
#include <vector>
#include <future>
using namespace std;
vector <double> res;
mutex flag;
void show(const double ret) {
	flag.lock();
	res.push_back(ret);
	cout << "我是:" << ret << endl;
	flag.unlock();
}

int main() {
	bool flag = 0;
	vector <double>ret = { 10,8,20,11,21,21.001 };
	thread n[6];
	for(int i=0 ; i<ret.size() ; i++)
		 n[i] = thread(show, ret[i]);
	cout << "应当先join再while" << endl;
	for (int i = 0; i < ret.size(); i++)
		n[i].join();
	while (1) {
		cout << "主线程任务"<<endl;
		Sleep(1000);
	}
	//cout << "验证先while再join能否执行到这里"<<endl;
	//for (int i = 0; i < ret.size(); i++)
	//	n[i].join();

}


在这里插入图片描述



版权声明:本文为weixin_45646601原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。