C++ std::thread线程及多线程间共享数据

2011年8月12日,国际标准化组织(ISO)发布了第三个C++标准,即ISO/IEC 14882:2011,简称ISO C++ 11标准。该标准第一次把线程的概念引入C++标准库。Windows平台运行的VS2012和Linux平台运行的g++4.7,都完美支持C++11线程。C++ 11线程的头文件是< thread >。

C++11线程的有关函数如下:
创建线程
std::thread::thread(Function&& f, Args&&… args);

等待线程结束
std::thread::join();

脱离线程控制
std::thread::detach();

交换线程
std::thread::swap(thread& other);

一、 简单线程示例

#include <thread>
#include <iostream>

void hello_world()
{
	std::cout << "hello, world!" << std::endl;
}

int main()
{
	std::thread t(hello_world);
	t.join();
}
[ycxie@fedora Workspace]$ g++ -o threaddemo threaddemo.cpp -pthread
[ycxie@fedora Workspace]$ ./threaddemo
hello, world!

二、 共享数据带来的问题

#include <thread>
#include <iostream>

int counter = 0;

void add()
{
	for (int i = 0; i < 1000000; i++)
	{
		counter = counter + 1;
	}
}

void sub()
{
	for (int i = 0; i < 1000000; i++)
	{
		counter = counter - 1;
	}
}

int main()
{
	std::thread t1(add);
	std::thread t2(sub);

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

	std::cout << "counter:\t" << counter << std::endl;
}
[ycxie@fedora Workspace]$ g++ -o threaddemo threaddemo.cpp -pthread
[ycxie@fedora Workspace]$ ./threaddemo
counter:	385798
[ycxie@fedora Workspace]$ ./threaddemo
counter:	194529

counter不为0,显然不是我们所期望的结果。

三、 使用互斥量解决

C++中通过实例化std::mutex创建互斥量实例,通过成员函数lock()对互斥量加锁,unlock()进行解锁。

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

int counter = 0;
std::mutex mtx; // 创建互斥量

void add()
{
	for (int i = 0; i < 1000000; i++)
	{
		mtx.lock(); // 加锁
		counter = counter + 1;
		mtx.unlock(); // 解锁
	}
}

void sub()
{
	for (int i = 0; i < 1000000; i++)
	{
		mtx.lock(); // 加锁
		counter = counter - 1;
		mtx.unlock(); // 解锁
	}
}

int main()
{
	std::thread t1(add);
	std::thread t2(sub);

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

	std::cout << "counter:\t" << counter << std::endl;
}
[ycxie@fedora Workspace]$ g++ -o threaddemo threaddemo.cpp -pthread
[ycxie@fedora Workspace]$ ./threaddemo
counter:	0
[ycxie@fedora Workspace]$ ./threaddemo
counter:	0

Leave a Reply

Your email address will not be published. Required fields are marked *