C++并发至南一


与 C++11 多线程相关的头文件

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

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

std::thread “Hello World”

例子:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
#include <stdio.h>
#include <stdlib.h>

#include <iostream> // std::cout
#include <thread> // std::thread

void thread_task()
{
    std::cout << "hello thread" << std::endl;
}
int main(int argc, const char * argv[]) {

    std::thread t(thread_task);
    t.join();
    
    return 0;
}