C++11 多线程

#include <thread>
#include <iostream>
#include <vector>
#include <mutex>


//线程测试一
void Test1(void);

//线程测试二
void Test2(void);

//线程测试三
void Test3(void);

//线程测试四
void Test4(void);

int main()
{
	//Test1();
	//Test2();
	//Test3();
	Test4();
	return 0;
}


void hello()
{
	std::cout << "Hello from thread " << std::endl;
	return;
}

//线程测试一
void Test1(void)
{
	//通过thread 类直接申明一个线程t1,参数是这个线程执行的回调函数的地址,通过jion()方法阻塞主线程,直到t1线程执行结束为止。
	std::thread t1(hello);
	t1.join();
	std::cout << "Main Thread" << std::endl;
	return;
	/*
	Hello from thread
	Main Thread
	请按任意键继续. . .
	*/
}

//线程测试二
void Test2(void)
{

	std::vector<std::thread> threads;
	for (int i = 0; i < 5; ++i)
	{
		threads.push_back(std::thread([](){std::cout << "Hello from lamda thread " << std::this_thread::get_id() << std::endl; }));
	}



	for (auto& thread : threads)
	{
		thread.join();
	}
	std::cout << "Main Thread" << "\t" << std::this_thread::get_id() << std::endl;
	return;

	/*
	Hello from lamda thread 1208
	Hello from lamda thread 3032
	Hello from lamda thread 1184
	Hello from lamda thread 4872
	Hello from lamda thread 4648
	Main Thread     2072
	请按任意键继续. . .
	*/
}

//线程测试三
void Test3(void)
{
	std::mutex m;
	std::thread t1([&m]()
	{

		std::this_thread::sleep_for(std::chrono::seconds(10));
		for (int i = 0; i < 10; i++)
		{
			m.lock();
			std::cout << "In t1 ThreadID : " << std::this_thread::get_id() << ":" << i << std::endl;
			m.unlock();
		}

	}
	);



	std::thread t2([&m]()
	{

		std::this_thread::sleep_for(std::chrono::seconds(1));

		for (int i = 0; i < 10; i++)
		{
			m.lock();
			std::cout << "In t2 ThreadID : " << std::this_thread::get_id() << ":" << i << std::endl;
			m.unlock();
		}

	}
	);

	t1.join();
	t2.join();

	std::cout << "Main Thread" << std::endl;
	return;
	/*
	In t2 ThreadID : 1864:0
	In t2 ThreadID : 1864:1
	In t2 ThreadID : 1864:2
	In t2 ThreadID : 1864:3
	In t2 ThreadID : 1864:4
	In t2 ThreadID : 1864:5
	In t2 ThreadID : 1864:6
	In t2 ThreadID : 1864:7
	In t2 ThreadID : 1864:8
	In t2 ThreadID : 1864:9
	In t1 ThreadID : 1848:0
	In t1 ThreadID : 1848:1
	In t1 ThreadID : 1848:2
	In t1 ThreadID : 1848:3
	In t1 ThreadID : 1848:4
	In t1 ThreadID : 1848:5
	In t1 ThreadID : 1848:6
	In t1 ThreadID : 1848:7
	In t1 ThreadID : 1848:8
	In t1 ThreadID : 1848:9
	Main Thread
	请按任意键继续. . .
	*/
}



struct Counter 
{
	std::mutex mutex;
	int value;
	Counter() : value(0) {}
	void increment()
	{
		// mutex.lock();                【1】表示没有使用锁
		++value;
		// mutex.unlock();              【1】
	}

	void decrement()
	{
		mutex.lock();
		--value;
		mutex.unlock();
	}
};
//线程测试四
void Test4(void)
{

	Counter counter;
	std::vector<std::thread> threads;

	for (int i = 0; i < 5; ++i)
	{

		threads.push_back(std::thread([&]()
		{
			for (int i = 0; i < 10000; ++i)
			{
				//counter.increment();
				counter.decrement();
			}
		}));
	}



	for (auto& thread : threads)
	{
		thread.join();
	}

	std::cout << counter.value << std::endl;
	return;
	/*
	由于创建线程是使用lambda表达式,并使用引用的方式访问counter这个变量,
	当没有使用lock来保护的时候(情况【1】),
	执行的结果可能不像预期的5000(程序的意思是每个线程使counter中的value自加1000次,5个线程运行结束的时候应该是5000),
	当没有使用锁的时候自加的操作可能被其他线程打断,因此结果可能会小于5000。
	*/
}

你可能感兴趣的:(C++11 多线程)