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

std::future returned from std::async hangs while going out of scope

I am using a combination of std::async and std::future from C++ 11. I am using to enforce a time_out on a certain activity that I do in my code which might take time as I try connecting to server. Following is how the code is: #include…
TheWaterProgrammer
  • 7,055
  • 12
  • 70
  • 159
2
votes
1 answer

C++ std::async does not spawn a new thread

C++11 int main(int argc, char** argv) { std::async(std::launch::async, [](){ while(true) cout << "async thread" <
Alston
  • 1,166
  • 2
  • 13
  • 26
2
votes
2 answers

parallell multiplication of vectors using async C++

I am trying to parallelize a piece of code that multiplies two vectors of complex floats and sums the result. To do this I am trying to use std::async with futures. My idea was to split the vector into 8 parts and perform the multiplication on each…
tallonj
  • 21
  • 4
2
votes
2 answers

Wait for multiple async tasks to complete

I am kicking off several async tasks like so: for(UINT Id: myIds) { std::async([=] { DoSomeStuffUsingId(Id); return true; }); } I have a function that needs to know if all these tasks have completed: void…
Harry Boy
  • 4,159
  • 17
  • 71
  • 122
2
votes
1 answer

asynchronous function producing inconsistent results

I have a function that runs asynchronously. Unfortunately, it only spits out the correct answer on occasion. The values represented by futures[i].get() change each time I run the code. I'm new to multithreading. double async_func() const { …
Dylan_Larkin
  • 503
  • 4
  • 15
2
votes
0 answers

Do std::async and std::future scale well on iOS and Android platforms?

I have a 2D particle system for a game engine in which I want to decouple the update loop of the particles from the main thread. I am using a thread pool implemented with boost::asio and am splitting up all the tasks into several stages and then…
M2tM
  • 4,415
  • 1
  • 34
  • 43
2
votes
0 answers

C++ async return value different when using x64

The following Code compiles using both Debug\Win32 and Debug \ x64 settings in VC2013, but when I use Debug \ x64 i get the following IntelliSense Error: IntelliSense: no operator "=" matches these operands operand types are:…
sgteam
  • 33
  • 1
  • 4
2
votes
1 answer

C++ std::async calling class member method doesn't see variable change

I am having some issue with a process that is being launched by std::async. class BaseClass { public: BaseClass() {enabledFlag = false;} virtual ~BaseClass() {} protected: int process(); bool enabledFlag; }; int…
John
  • 10,837
  • 17
  • 78
  • 141
2
votes
1 answer

Odd behavior with std::async

Consider the following sample code: #include #include #include typedef std::array foo_t; foo_t* bar(foo_t& foo) { return &foo; } int main() { foo_t foo; auto a = std::async(bar, foo); auto b =…
Robert Mason
  • 3,949
  • 3
  • 31
  • 44
2
votes
1 answer

C++ async threads terminate when calling thread finishes

I am trying to do a recursive directory listing using a multi threaded approach. The following code works fine when replacing the async calls as a normal single threaded recursive function call but when implemented with async, the recursively…
2
votes
1 answer

What happens to std::async call if parent/main thread dies

If I am right, the std::async uses a new thread and calls the method in it. I was wondering what happens if the main thread or the parent thread dies. Does the thread controlling the async method dies as well.
Adobri
  • 471
  • 7
  • 15
2
votes
1 answer

Using nested std::async with std::condition_variable in Visual Studio 2012

I have got Worker classes and a Handler class to create an abstraction layer for jobs already. I wanted to use std::async to pour some asynchrony into the mix but I got some weird behaviour from my Visual Studio 2012 (update 1). My class hierarchy…
zahir
  • 1,298
  • 2
  • 15
  • 35
1
vote
1 answer

std::async on c++20 no matching function

I have the following code snippets and when compiling with c++20, it is failing with the following error msg, no matching function for call to async and would not yield a useful error message. Proper header files are included. driver.cpp:40:28:…
vtsam
  • 13
  • 2
1
vote
0 answers

Is there a need to provide a std::mutex for std::async?

As stuff I mentioned above, Should I prodive a std::mutex for std::async in the following section? : #include #include #include #include int main() { [[maybe_unused]] result=::std::async([&] { …
Savner_Dig
  • 21
  • 1
  • 8
1
vote
0 answers

Occasional false positive memory leak when using std::future and _CrtMemDifference on Windows

Below is a minimal repro of the memory leak I see when using std::future. Inside ParallelForEach I create an array of std::future with a subsequent get that should block the execution of the main thread until all the tasks for vec are finished. Once…