c++ 11 多线线程系列-----thread

一、与 C++11 多线程相关的头文件地方

C++11 新标准中引入了四个头文件来支持多线程编程,他们分别是 ,,,

  • :该头文主要声明了两个类, std::atomic 和 std::atomic_flag,另外还声明了一套 C 风格的原子类型和与 C 兼容的原子操作的函数。
  • :该头文件主要声明了 std::thread 类,另外 std::this_thread 命名空间也在该头文件中。
  • :该头文件主要声明了与互斥量(mutex)相关的类,包括 std::mutex 系列类,std::lock_guard, std::unique_lock, 以及其他的类型和函数。
  • :该头文件主要声明了与条件变量相关的类,包括 std::condition_variable 和 std::condition_variable_any。
  • :该头文件主要声明了 std::promise, std::package_task 两个 Provider 类,以及 std::future 和 std::shared_future 两个 Future 类,另外还有一些与之相关的类型和函数,std::async() 函数就声明在此头文件中。
本篇博文研究的是thread头文件中的类:

我们来看第一个利用c++11特性创建线程的例子:
        
#include 
#include 

using namespace std;

void th_function()
{
	std::cout << "hello thread." << std::endl;
}

int main(int argc, char *argv[])
{
	std::thread t(th_function);
	t.join();

	return 0;
}



二、现在我们来看看c++ 11中thread头文件中的thread类:

c++ 11 多线线程系列-----thread_第1张图片

三、std::thread 构造

default (1)
thread() noexcept;
initialization (2)
template 
explicit thread (Fn&& fn, Args&&... args);
copy [deleted] (3)
thread (const thread&) = delete;
move (4)
thread (thread&& x) noexcept;

(1). 默认构造函数,创建一个空的 thread 执行对象。
(2). 初始化构造函数,创建一个 thread对象,该 thread对象可被 joinable,新产生的线程会调用 fn 函数,该函数的参数由 args 给出。
(3). 拷贝构造函数(被禁用),意味着 thread 不可被拷贝构造。
(4). move 构造函数,move 构造函数,调用成功之后 x 不代表任何 thread 执行对象。
注意:可被 joinable 的 thread 对象必须在他们销毁之前被主线程 join 或者将其设置为 detached.

看一个例子:
   这个例子中创建了两个线程,其中一个线程函数带参数,大家仔细分析上面的thread类的构函数,看如何创建线程。
// thread example
#include        // std::cout
#include          // std::thread

void thr_function1()
{
	for (int i = 0; i != 10; ++i)
	{
		std::cout << "thread 1 print " << i << std::endl;
	}
}

void thr_function2(int n)
{
	std::cout << "thread 1 print " << n << std::endl;
}

int main()
{
	std::thread t1(thr_function1);     // spawn new thread that calls foo()
	std::thread t2(thr_function2, 111);  // spawn new thread that calls bar(0)

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

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

	std::cout << "thread 1 and htread 2 completed.\n";

	return 0;
}
c++ 11 多线线程系列-----thread_第2张图片

大家的输出可能不是这样,有可能是下面这样的,不要紧,因为这个例子中没使用多线程的异步机制,线程之间存在竞争,看稍后文章讲解所以输出可能是下面这样的情况。

c++ 11 多线线程系列-----thread_第3张图片

三、move 赋值操作

move (1)
thread& operator= (thread&& rhs) noexcept;
copy [deleted] (2)
thread& operator= (const thread&) = delete;
  • (1). move 赋值操作,如果当前对象不可 joinable,需要传递一个右值引用(rhs)给 move 赋值操作;如果当前对象可被 joinable,则 terminate() 报错。
  • (2). 拷贝赋值操作被禁用,thread 对象不可被拷贝。
看下面的例子。

// example for thread::operator=
#include        // std::cout
#include          // std::thread, std::this_thread::sleep_for
#include          // std::chrono::seconds

void pause_thread(int n)
{
	std::this_thread::sleep_for(std::chrono::seconds(n));//c++11 this_thread 类
	std::cout << "pause of " << n << " seconds ended\n";
}

int main()
{
	std::thread threads[5];                         // default-constructed threads

	std::cout << "Spawning 5 threads...\n";
	for (int i = 0; i<5; ++i)
		threads[i] = std::thread(pause_thread, i + 1);   // move-assign threads 这里调用move复制函数

	std::cout << "Done spawning threads. Now waiting for them to join:\n";
	for (int i = 0; i<5; ++i)
		threads[i].join();

	std::cout << "All threads joined!\n";

	return 0;
}
c++ 11 多线线程系列-----thread_第4张图片


参考资料:http://www.cplusplus.com/reference/thread/thread/
                    http://www.cnblogs.com/haippy/p/3236136.html


                

你可能感兴趣的:(c++,11,多线线程系列,thread类,c++,11,多线线程系列)