Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

std::async fails for std::atomic_ref::wait?

std::async works for own type and other std types like thread, why does it fail here for std::atomic_ref ?

#include <future>
#include <iostream>
#include <atomic>
#include <thread>

struct Dummy
{
  Dummy() : value(0) {}
  void add(int i) { value +=i;}
  int value;
};

void func(){}

int main()
{
  // Dummy::add
  Dummy foo;
  auto f = std::async(&Dummy::add, &foo, 77);
  f.get();
  std::cout << foo.value << "\n";
  
  // std::thread::join
  std::thread t(func);
  auto f2 = std::async(&std::thread::join, &t);
  
  // std::atomic_ref::wait
  size_t a = 0;
  std::atomic_ref<size_t> atom(a);
  atom.wait(1); // OK
  auto f1 = std::async(&std::atomic_ref<size_t>::wait, &atom, 1); // error
}

run

<source>:29:23: error: no matching function for call to 'async(void (std::__atomic_ref<long unsigned int, true, false>::*)(long unsigned int, std::memory_order) const noexcept, std::atomic_ref<long unsigned int>*, int)'

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Function pointers (including member function pointers) don’t support default arguments, they are silently ignored.

std::atomic_ref::wait has a second parameter with a default argument: std::memory_order order = std::memory_order::seq_cst, and when calling through a member-pointer, it needs to be specified manually:

auto f1 = std::async(&std::atomic_ref<size_t>::wait, &atom, 1, std::memory_order_seq_cst);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading