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
179
votes
4 answers

What is the difference between packaged_task and async

While working with the threaded model of C++11, I noticed that std::packaged_task task([](int a, int b) { return a + b; }); auto f = task.get_future(); task(2,3); std::cout << f.get() << '\n'; and auto f =…
nikolas
  • 8,707
  • 9
  • 50
  • 70
96
votes
6 answers

When to use std::async vs std::threads?

Can anybody give a high level intuition about when to use each of them? References: Is it smart to replace boost::thread and boost::mutex with c++11 equivalents? When is it a good idea to use std::promise over the other std::thread mechanisms?
Javi
  • 3,440
  • 5
  • 29
  • 43
93
votes
5 answers

Why should I use std::async?

I'm trying to explore all the options of the new C++11 standard in depth, while using std::async and reading its definition, I noticed 2 things, at least under linux with gcc 4.8.1 : it's called async, but it got a really "sequential behaviour",…
user2485710
  • 9,451
  • 13
  • 58
  • 102
57
votes
6 answers

Can I use std::async without waiting for the future limitation?

High level I want to call some functions with no return value in a async mode without waiting for them to finish. If I use std::async the future object doesn't destruct until the task is over, this make the call not sync in my case. Example void…
Roee Gavirel
  • 18,955
  • 12
  • 67
  • 94
49
votes
1 answer

Which std::async implementations use thread pools?

One of the advantages of using std::async instead of manually creating std::thread objects is supposed to be that std::async can use thread pools under the covers to avoid oversubscription problems. But which implementations do this? My…
KnowItAllWannabe
  • 12,972
  • 8
  • 50
  • 91
28
votes
1 answer

Is the Visual C++ implementation of std::async using a thread pool standard-compliant?

Visual C++ uses the Windows thread pool (Vista's CreateThreadpoolWork if available and QueueUserWorkItem if not) when calling std::async with std::launch::async. The number of threads in the pool is limited. If we create several tasks that run for a…
conio
  • 3,681
  • 1
  • 20
  • 34
21
votes
1 answer

behaviour of std::async(std::launch::deferred) + std::future::then

The idea behind a deferred future (achieved only by calling std::async with std::launch::deferred flag) is that the callback is called only when someone tries to wait or to pull the futuristic value or exception of the future. by then the callback…
David Haim
  • 25,446
  • 3
  • 44
  • 78
20
votes
2 answers

Why is std::async slow compared to simple detached threads?

I've been told several times, that I should use std::async for fire & forget type of tasks with the std::launch::async parameter (so it does it's magic on a new thread of execution preferably). Encouraged by these statements, I wanted to see how…
krispet krispet
  • 1,648
  • 1
  • 14
  • 25
19
votes
7 answers

How can a function run "as if" on a new thread without doing so?

Per [futures.async]/3 bullet 1 of the C++ Standard, when a function f is passed to std::async with the std::launch::async launch policy, f will run "as if in a new thread of execution". Given that f can do anything, including loop infinitely and…
KnowItAllWannabe
  • 12,972
  • 8
  • 50
  • 91
18
votes
2 answers

What is the issue with std::async?

Near the beginning of this clip from C++ And Beyond, I heard something about problems with std::async. I have two questions: For a junior developer, is there a set of rules for what to do and what to avoid when using std::async? What are the…
NoSenseEtAl
  • 28,205
  • 28
  • 128
  • 277
17
votes
2 answers

c++11 async continuations or attempt at .then() semantics

The code below is based on Herb Sutter's ideas of an implementation of a .then() type continuation. template auto then(Fut f, Work w)->std::future { return std::async([=] { w(f.get()); });…
dirvine
  • 57,399
  • 2
  • 18
  • 19
14
votes
2 answers

std::future as a parameter to a function C++

Consider following code void printPromised(std::future f) { std::cout << f.get() << std::endl; } int main() { printPromised(std::async(std::launch::async, [](){ return 8; })); // This works auto f = std::async(std::launch::async,…
Amith Chinthaka
  • 1,015
  • 1
  • 17
  • 24
12
votes
2 answers

std::async - Implementation dependent usage?

I've been thinking about std::async and how one should use it in future compiler implementation. However, right now I'm a bit stuck with something that feels like a design flaw. The std::async is pretty much implementation dependent, with probably…
ronag
  • 49,529
  • 25
  • 126
  • 221
12
votes
2 answers

std::async with overloaded functions

Possible Duplicate: std::bind overload resolution Consider following C++ example class A { public: int foo(int a, int b); int foo(int a, double b); }; int main() { A a; auto f = std::async(std::launch::async, &A::foo, &a, 2,…
Amith Chinthaka
  • 1,015
  • 1
  • 17
  • 24
12
votes
1 answer

Perfect Forwarding to async lambda

I have a function template, where I want to do perfect forwarding into a lambda that I run on another thread. Here is a minimal test case which you can directly compile: #include #include #include #include…
Alexander Kondratskiy
  • 4,156
  • 2
  • 30
  • 51
1
2 3
10 11