Questions tagged [stdasync]

The C++11 function template std::async() provides a mechanism to launch a function potentially in a new thread and provides the result of the function in a future object.

C++11 provides the function template std::async() as a simple tool for running functions asynchronously. Its parameters are a callable object and an optional list of arguments and its return type is an instantiation of the std::future class template.

Rather than invoking the target object immediately it will be called asynchronously, either in a new thread or deferred until the result is needed. The std::future object can be used to retrieve the result of the asynchronous call or any exceptions it throws.

The initial proposals for std::async() were N2889 An Asynchronous Call for C++ and A simple async().

See also

163 questions
1
vote
1 answer

How to properly delete a pointer to callback function.

I have a MainProgram.exe which calls in to MyDll.dll and uses curl to receive data on a callback function. I have wrapped curl in a function called CurlGetData which creates a curl instance and performs curl_easy_perform. Here is my…
Harry Boy
  • 4,159
  • 17
  • 71
  • 122
1
vote
1 answer

Is std::launch::async policy needed?

What is the difference in the following: std::async(my_function); and std::async(std::launch::async, my_function); What is the difference in using the pilicy std::launch::async in this case?? Does the first option not launch the function…
Harry Boy
  • 4,159
  • 17
  • 71
  • 122
1
vote
1 answer

Is this code correctly synchronized?

I wonder if this code is fine or not: #include #include struct Foo { Foo() :m_a(0) { } int m_a; }; int main() { Foo f; auto handle = std::async( std::launch::async, …
qdii
  • 12,505
  • 10
  • 59
  • 116
1
vote
1 answer

std::async, std::function object and templates with 'callable' parameter

#include #include void z(int&&){} void f1(int){} void f2(int, double){} template void g(Callable&& fn) { fn(123); } template std::future async_g(Callable&& fn) { return…
PowerGamer
  • 2,106
  • 2
  • 17
  • 33
1
vote
1 answer

Speeding up std:.vector filling with new c++11 std::async

i'm using vc++ 2013 express edition. I'm studying some new feature of c++11 like std::async, and std::future. I have a class Foo, with an std::shared_ptr >. In Foo ctor i use std::make_shared to allocate in the heap the…
1
vote
2 answers

Detect thread end

I would like to detect thread end in c++11 but I don't know how to do, it's look like that "get" block program, here is what I've done: void Object::init() { this->thread = std::async( std::launch::async, &State::load, stateInstance ); } /* A…
LongDuZboub
  • 1,041
  • 2
  • 12
  • 18
1
vote
1 answer

std::async doesn't seem to spawn thread with std::launch::async

I am writing a DCPU-16 emulator and I am calculating the real time clock speed of the CPU by launching a thread that calls the function getRealTimeCPUClock() in a separate thread. The problem is it seems that the future object's "valid" attribute…
DanB91
  • 1,399
  • 1
  • 13
  • 23
0
votes
0 answers

The function called by std::async is not executed immediately?

#include #include auto gClock = clock(); char threadPool(char c) { std::cout << "enter thread :" << c << " cost time:" << clock() - gClock << std::endl; std::this_thread::sleep_for(std::chrono::seconds(2)); for (int…
YuHuanTin
  • 1
  • 1
0
votes
0 answers

Class member function with argument run asynchronously

I am trying to asynchronously run a member function, but I seem to have issues with every attempt. Here is my code example: A::A(){} A::~A(){} void A::asyncFunc(int i) { std::cout<< i ; } void A::otherFunc() { std::future t; …
Zefelix
  • 13
  • 4
0
votes
1 answer

c++ No instance of overloaded function "std::async" matches argument list

i'm trying to run a listener Method async with std::async but i get the following error: No instance of overloaded function "std::async" matches argument list auto listener = std::async(std::launch::async, server.Listen()); The server.Listen()…
Oliver Karger
  • 105
  • 1
  • 11
0
votes
1 answer

Using std::transform with std::async

Why the commented lines of the following does not compile? Can I use std::transform with async? For testing it is hard coded. Finally, I want to to use it for millions of points. #include #include #include #include…
qqqqq
  • 837
  • 12
  • 31
0
votes
2 answers

Why calling future.get() retrieved from async leads to unexpected output order

Consider the following code: #include #include #include #include #include std::mutex mutex; int generate() { static int id = 0; std::lock_guard lock(mutex); id++; std::cout…
b2mb0e
  • 31
  • 5
0
votes
0 answers

Real world example of getting value from std::future before std::thread::join is called

I start learning about std::thread and std::future, and wrote very trivial example (I wanted to get the value from std::future before std::thread ends): #include #include #include #include int foo(int a, int…
0
votes
0 answers

Parallel construction/instantiation in c++

I have a class that has many setter methods. Each subsequent setter method requires the data that has been set by the preceding setter method. i.e. if setA() has set the variable _a in the class, then setB() will require the variable _a If not…
0
votes
0 answers

std::launch::async runs sequentially when compiled as R-package

I have a problem with the behavior of std::launch::async I have two cases where I use std::launch::async: Case 1: const AnotherClass AnyClass::train(args) const{ // first do some stuff std::vector thread_ranges; …
sandroh
  • 1
  • 1