C++11 thread 生成隨機數的方法





#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<thread>
#include<iostream>
#include<atomic>
#include<mutex>
#include<time.h>
using namespace std;
mutex m;
time_t t;
void test(int seed,int depth,clock_t _time) {
	srand(_time + seed);
	m.lock();
	cout << rand() << endl;
	m.unlock();
	if (depth-->0) {
		test(seed, depth, time(&t) +rand());
	}
}

int main()
{
	thread t1(test,12012,100,time(&t));
	thread t2(test,25301,100, time(&t));
	t1.join();
	t2.join();
	return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章