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
5
votes
2 answers

timeout in std::async

Is there a way to implement timeout in the std::async method, so I want this call to timeout and complete if the thread hasnt completed for the specified amount of time. How can I implement this functionality.
Adobri
  • 471
  • 7
  • 15
4
votes
1 answer

error: no member named 'async' in namespace 'std'

I know that std::async is a C++11 thing but I am pretty sure that my compiler has C++11 support. #include using namespace::std; void functionToRun() { // some code } int main() { auto x = 2; // throws warning warning: 'auto' type…
coda
  • 2,188
  • 2
  • 22
  • 26
4
votes
2 answers

Can long-running std::asyncs starve other std::asyncs?

As I understand it, usual implementations of std::async schedule these jobs on threads from a pre-allocated thread pool. So lets say I first create and schedule enough long-running std::asyncs to keep all threads from that thread pool occupied.…
Dreamer
  • 1,139
  • 9
  • 18
4
votes
2 answers

Trouble with multiple std::threads and main program execution

I have been struggling for days to come up with a mechanism for launching a few timers and not having it clock the main program execution. Combinations of .join() and .detach(), wait_until(), etc What I have is a vector of std::thread and I want…
Jasmine
  • 15,375
  • 10
  • 30
  • 48
4
votes
1 answer

Given a container of futures, how to perform all gets in a non-blocking way?

So, I'm trying to create a generic way to both create a container of futures, as well as perform all the future.get()' in a non-blocking way. I anticipate that the completion time the tasks will take should range from a few hundred milliseconds, up…
3
votes
1 answer

Understanding Scope and Lifetime of References in std::async within a Loop

My question centers around the for-loop in the listDirs function, where I am launching async tasks. I am passing path by reference to std::async which then invokes the listDir function in a separate thread. I am aware that once the for-loop moves to…
Sami
  • 513
  • 4
  • 11
3
votes
0 answers

Can code flow move ahead before future get method returns?

Is it possible that "Main end" could get displayed before all result.get(); are returned back in below code snippet (Under any scenario)? OR "Main end" will always be the last one to appear? #include #include #include…
Helena
  • 444
  • 2
  • 15
3
votes
2 answers

Execute function in C++ after asynchronous 60 sec delay?

I've read this can be achieved using std::this_thread::sleep_for and std::async, but it's not working for me. Here is the function to be called: bool Log::refresh_data() { std::this_thread::sleep_for( std::chrono::minutes( 1 ) ); …
LearnMore
  • 695
  • 1
  • 5
  • 12
3
votes
1 answer

How to return std::tuple from a std::async task

How can I launch a member function as a std::async task, which returns a std::tuple. Sample code: #include #include #include #include class Foo { bool calc; public: Foo(bool b) : calc(b) {} …
3
votes
1 answer

VS2015 std::async strange

In the below code in VS2015, I'm getting acefbd in the first line, which is correct. but in the 2nd test where I seperate out into individual lines, the output is abcdef. Is that intended behavior? #include #include using…
Robin
  • 451
  • 3
  • 14
3
votes
0 answers

C++, Visual Studio: unexpected std::async return type

I'm trying to save multiple std::futures in a QMap. The QMap is a member variable of my class: QMap> threads; I now want to store the results of multiple std::async calls in this map, like…
bweber
  • 3,772
  • 3
  • 32
  • 57
3
votes
1 answer

How to properly return large data from a std::future in c++11

I'm a bit puzzled what is the proper way to return large data from an async function in c++. Take for example this code. It creates a large vector in a function and returns the allocated vector. #include #include #include…
user1978011
  • 3,419
  • 25
  • 38
3
votes
1 answer

async uses a threadpool?

I am new to C++ and I tried using std::async with launch::async to spawn new threads to handle incoming UDP packets. For every new request on a particular port, async spawns a new thread to handle it. Under load, I find that the udp packets were…
sethu
  • 1,691
  • 4
  • 22
  • 34
2
votes
1 answer

Explicit copy constructor of a parameter passed via std::async

In the following program foo function is executed asynchronously and its argument of type A is copied inside async by value: #include struct A { A() = default; explicit A(const A &) = default; }; void foo(const A &) {} int main()…
Fedor
  • 17,146
  • 13
  • 40
  • 131
2
votes
1 answer

Callable class objects in C++ : no matching function for call to ‘std::tuple::tuple()’

My code consists of 2 files: main.cpp and utils.hpp. The contents of those files are given below: utils.hpp #ifndef UTILITY_HPP #define UTILITY_HPP #include #include #include #include #include…
1 2
3
10 11