Does future :: wait () support synchronization - with the completion of the async () thread of execution?

He said that he was thread::join()synchronizing with the completion of the corresponding thread of execution. I am wondering if this applies to async()and future::wait(). For example:

std::atomic_int v(0);
std::async(
  std::launch::async,
  [&v] { v.fetch_add(1, std::memory_order_relaxed); }
).wait();
assert(v.load(std::memory_order_relaxed) == 1);

assert() will never work.

+4
source share
1 answer

Straight from N3337 (standard draft), [futures.async] / 5 with emphasis:

Synchronization: regardless of the policy argument provided,

  • the async call is synchronized with the ([intro.multithread]) call of f. [Note: this statement applies even when the corresponding future object moves to another thread. - final note]; and

  • f ([intro.multithread]) . [: f , . - ]

launch::async,

  • , , , , ([Thread.thread.member]);

  • ([intro.multithread]) , , , , .

, , , , .

+4

Source: https://habr.com/ru/post/1694570/


All Articles