受欢迎的博客标签

C++ 11--项目学习笔记

Published

C++11标准之前,是没有多线程支持的。在这种情况下,Linux/Unix平台下的开发者通常会使用系统自带的pthread,第三方库POSIX Threads,Windows上的开发者也会使用_cbegintread,CreateThread(Win api)和AFXBeginThread(only for MFC)。但很明显,这些API都只针对特定的操作系统平台,可移植性较差。如果要同时支持Linux和Windows系统,你可能要写两套代码。

C++标准并没有提供对多进程并发的原生支持,所以C++的多进程并发要靠其他API——这需要依赖相关平台。


C++11 标准提供了一个新的线程库thread,内容包括了管理线程、保护共享数据、线程间的同步操作、低级原子操作等各种类。标准极大地提高了程序的可移植性,以前的多线程依赖于具体的平台,而现在有了统一的接口进行实现。

C++11 新标准中引入了几个头文件来支持多线程编程:(所以我们可以不再使用 CreateThread 来创建线程,简简单单地使用 std::thread 即可。)

< thread > :包含std::thread类以及std::this_thread命名空间。管理线程的函数和类在 中声明.
< atomic > :包含std::atomic和std::atomic_flag类,以及一套C风格的原子类型和与C兼容的原子操作的函数。
< mutex > :包含了与互斥量相关的类以及其他类型和函数
< future > :包含两个Provider类(std::promise和std::package_task)和两个Future类(std::future和std::shared_future)以及相关的类型和函数。
< condition_variable > :包含与条件变量相关的类,包括std::condition_variable和std::condition_variable_any。

< thread >

C++ 11创建线程
创建线程非常的简单的,下面就是一个使用了多线程的Hello World示例:

#include <iostream>
#include <thread> // ①

using namespace std; // ②

void hello() { // ③
  cout << "Hello World from new thread." << endl;
}

int main() {
  thread t(hello); // ④
  t.join(); // ⑤

  return 0;
}

为了使用多线程的接口,我们需要#include <thread>头文件。
为了简化声明,本文中的代码都将using namespace std;。
新建线程的入口是一个普通的函数,它并没有什么特别的地方。
创建线程的方式就是构造一个thread对象,并指定入口函数。与普通对象不一样的是,此时编译器便会为我们创建一个新的操作系统线程,并在新的线程中执行我们的入口函数。

come rrom:https://paul.pub/cpp-concurrency/

 

thread::get_id

Example

// thread::get_id / this_thread::get_id
#include <iostream>       // std::cout
#include <thread>         // std::thread, std::thread::id, std::this_thread::get_id
#include <chrono>         // std::chrono::seconds
 
std::thread::id main_thread_id = std::this_thread::get_id();

void is_main_thread() {
  if ( main_thread_id == std::this_thread::get_id() )
    std::cout << "This is the main thread.\n";
  else
    std::cout << "This is not the main thread.\n";
}

int main() 
{
  is_main_thread();
  std::thread th (is_main_thread);
  th.join();
}

output

This is the main thread.
This is not the main thread.

 

< condition_variable >

C++11多线程-条件变量(std::condition_variable)

 

C++标准库启动线程的方法可分为三类:

1.async

2.thread

3.packaged_task。

其中:

1.async一般和futrue或者shared_future搭配,

2.thread一般和promise搭配(也要结合future使用),

3.packaged_task一般和future搭配。

 

c++11的future+async

https://blog.csdn.net/gaussrieman123/article/details/81779329

其他两类见:

C++ 并发编程(从C++11到C++17)

https://paul.pub/cpp-concurrency/

 

 

C++ 11--项目学习笔记

https://blog.csdn.net/qq_29797957/category_9229731.html

The Microsoft concurrency namespace, provided via #include <ppltasks.h>, is an alternative to C++11 threads. The concurrency namespace functions are at a higher level of abstraction than C++11 threads and are easier to use.

c++11 多线程(3)atomic 

https://www.jianshu.com/p/8c1bb012d5f8

c++11 boost::thread编程实战(3)——通过PostThreadMessage和PeekMessage实现线程通信

http://www.manongjc.com/article/64482.html

C++11 中的并发与多线程

https://www.cnblogs.com/yskn/p/9355556.html

C++ 并发编程(从C++11到C++17)改造for循环

https://paul.pub/cpp-concurrency/

C++11 std::thread

https://www.runoob.com/w3cnote/cpp-std-thread.html