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
11
votes
1 answer

How can I cancel a std::async function?

Possible Duplicate: Is there a way to cancel/detach a future in C++11? There is a member function which runs asynchronously using std::future and std::async. In some case, I need to cancel it. (The function loads near objects consecutively and…
danijar
  • 32,406
  • 45
  • 166
  • 297
11
votes
5 answers

c++11 std::async doesn't work in mingw

Running this code from Herb Sutter's presentation. This works fine in linux under gcc 4.6.3. I'm thinking that future.h isn't supported in mingw, but the error is really hard to understand! #include #include #include…
BHP
  • 985
  • 2
  • 14
  • 18
8
votes
3 answers

Why std::future is different returned from std::packaged_task and std::async?

I got to know the reason that future returned from std::async has some special shared state through which wait on returned future happened in the destructor of future. But when we use std::pakaged_task, its future does not exhibit the same…
gaurav bharadwaj
  • 1,669
  • 1
  • 12
  • 29
8
votes
0 answers

Does the future returned from std::async with the launch::deferred policy block in the destructor?

In the description of the std::async it is said that: If the std::future obtained from std::async is not moved from or bound to a reference, the destructor of the std::future will block at the end of the full expression until the asynchronous…
Dmitry Kuzminov
  • 6,180
  • 6
  • 18
  • 40
8
votes
3 answers

main thread waits for std::async to complete

I am using std::async to create a thread, I want this new thread should execute separately and main thread should not wait for it. But here when I call std::async, a new thread is created but main thread is waiting for completion of fun(). I want…
aromahola
  • 190
  • 1
  • 12
8
votes
1 answer

"no matching function for call to ‘async(std::launch, , std::string&)’"

I am trying to create a thread using std::async, but I keep getting the error "no matching function for call to ‘async(std::launch, , std::string&)’" on the line ConnectFuture = std::async(std::launch::async,…
Andres La
  • 306
  • 1
  • 3
  • 9
8
votes
2 answers

using std::async with template functions

How can I, or, can I, pass a template function to async? Here is the code: //main.cpp #include #include #include #include int main () { std::vector v(16,1); auto r0 = …
silvermangb
  • 355
  • 2
  • 11
7
votes
1 answer

How to create an already-resolved future

I have a function that returns a std::future. I have added a cache to the implementation, and I would like to optionally return a value immediately if it does not need to be recalculated. How can I create an already-resolved future? // Class…
Carson
  • 2,700
  • 11
  • 24
7
votes
2 answers

What is the lifetime of the arguments of std::async?

It appears that arguments of a function executed via std::async share the lifetime of the future: #include #include #include struct S { S() { std::cout << "S() " << (uintptr_t)this << std::endl; } …
Kentzo
  • 3,881
  • 29
  • 54
7
votes
1 answer

Is std::async guaranteed to be called for functions returning void?

I've wrote the following code to test std::async() on functions returning void with GCC 4.8.2 on Ubuntu. #include #include void functionTBC() { std::cerr << "Print here\n"; } int main(void) { #ifdef USE_ASYNC auto i =…
timrau
  • 22,578
  • 4
  • 51
  • 64
7
votes
1 answer

Can std::async call std::function objects?

Is it possible to call function objects created with std::bind using std::async. The following code fails to compile: #include #include #include using namespace std; class Adder { public: int add(int x, int y)…
Huhwha
  • 575
  • 5
  • 15
6
votes
1 answer

Why will for-loop with multithreading not have as great performance as with single-thread?

I believed it was better to process simple and heavy works (ex. matrix-calculation) with multi-threading than with single-thread, so I tested the following code : int main() { constexpr int N = 100000; std::random_device rd; …
m. bs
  • 81
  • 1
  • 8
5
votes
1 answer

std::async vs. thread

I am trying to understand how exactly async differs from using threads. On a conceptual level, I thought multithreading was by definition asynchronous, because you are doing context switches between threads for things like I/O. But it seems that…
Victor M
  • 603
  • 4
  • 22
5
votes
1 answer

c++11 threads vs async

Consider the following two snippets of code where I am trying to launch 10000 threads: Snippet 1 std::array, 10000> furArr_; try { size_t index = 0; for (auto & fut : furArr_) { …
Arun
  • 3,138
  • 4
  • 30
  • 41
5
votes
1 answer

How standard is std::thread?

I've noticed that on a lot of the classic C++ reference sources that HAVE been updated for C++11, such as cplusplus.com and the Josuttis Standard Library Reference book, don't seem to cover / have any documentation at all on the C++11 concurrency…
David Adrian
  • 1,079
  • 2
  • 9
  • 24
1
2
3
10 11