阅读背景:

C++ 11新特性 thread开放线程

来源:互联网 
// ThreadTest.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
#include <thread>
#include <mutex>
using namespace std;

mutex m;

void foo(int a)
{
	m.lock();//使用同步锁
	this_thread::sleep_for(chrono::seconds(2));//延时十秒
	printf("11111111111111111");
	m.unlock();
}

void bar(int x)
{
	// do stuff...
}

int main()
{
	thread first(foo,1);     // spawn new thread that calls foo()
	thread second(bar, 0);  // spawn new thread that calls bar(0)

	std::cout << "main, foo and bar now execute concurrently...\n";

	// synchronize threads:
	first.join();                // pauses until first finishes
	second.join();               // pauses until second finishes

	std::cout << "foo and bar completed.\n";
	cin.get();
	return 0;
}
// ThreadTest.cpp : 定义控制台应用程序的入口点。
//

#include



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: