【C++并发与多线程】 11_std::atomic叙谈、std::launch(std::async) 深刻

std::atomic 叙谈

  • 原子类模板彻底专用于基本整数类型(bool除外),以及 <cstdint> 中 typef 所需的任何扩展整数类型。

专用的基本数据类型:ios

char
signed char
unsigned char
short
unsigned short
int
unsigned int
long
unsigned long
long long
unsigned long long
char16_t
char32_t
wchar_t
extended integral types (if any)

附加的成员函数:segmentfault

atomic::fetch_add
atomic::fetch_sub
atomic::fetch_and
atomic::fetch_or
atomic::fetch_xor
atomic::operator++
atomic::operator--
operator (comp. assign.)
  • 对于 bool 实例化,仅支持常规原子操做。请注意,大多数 c-style 原子类型都是这些专门化的别名(或这些专门化继承的基类的别名)

原子操做还部分专用于指针类型,具备一下附加成员函数:异步

atomic::fetch_add
atomic::fetch_sub
atomic::operator++
atomic::operator--
operator (comp. assign.)

std::launch(std::async) 深刻理解

std::asyncasync

enum class launch : /* unspecified */ {
    async =    /* unspecified */,
    deferred = /* unspecified */,
    /* implementation-defined */
};
  • 此枚举类型用于定义 aync 异步调用中的启动策略。

launch::async

  • 入口函数由新线程异步调用,并将其返回其与共享状态的访问点同步。
#include <iostream>
#include <future>
#include <thread>

using namespace std;

bool async_func () {

    cout << "async_func begin " << std::this_thread::get_id() << endl;

    cout << "async_func end" << endl;

    return true;
}

int main ()
{
  cout << "main begin " << std::this_thread::get_id() << endl;

  std::future<bool> fut = std::async (launch::async, async_func);   // 建立新线程并调用线程入口函数

  cout << fut.get() << endl;

  cout << "main end" << endl;

  return 0;
}

输出:函数

main begin 1
async_func begin 2
async_func end
1
main end

launch::deferred

  • 入口函数延迟调用。
  • 在调用线程访问 future 对象的 wait, get 函数时,入口函数才被调用。
  • 同时未建立新线程,入口函数在 future 对象被调用的线程中执行。
#include <iostream>
#include <future>
#include <thread>

using namespace std;

bool async_func () {

    cout << "async_func begin " << std::this_thread::get_id() << endl;

    cout << "async_func end" << endl;

    return true;
}

int main ()
{
  cout << "main begin " << std::this_thread::get_id() << endl;

  std::future<bool> fut = std::async (launch::deferred, async_func);

  if (fut.wait_for(std::chrono::microseconds(0)) == std::future_status::deferred)   // 检查是不是 launch::deferred 
  {
    cout << "std::future_status::deferred" << endl;
  }

  cout << fut.get() << endl;    // 入口函数被调用

  cout << "main end" << endl;

  return 0;
}

输出:[注意两处打印的线程id]fetch

main begin 1
std::future_status::deferred
async_func begin 1
async_func end
1
main end

launch::async | launch::deferred

  • 依据系统和库的实现,自动选择启动策略。
  • 准则主要为是否有足够的系统资源建立线程
  • 这也是未明确指定参数时的默认实现。
std::future<bool> fut = std::async (launch::async|launch::deferred, async_func);

==

std::future<bool> fut = std::async (async_func);
相关文章
相关标签/搜索