Questions tagged [std-future]

The [c++11] std::future object can be used to retrieve the result of the asynchronous operations or any exceptions it throws.

An asynchronous operation can be created via:

  • std::async
  • std::packaged_task
  • std::promise

Each of the above can provide a std::future object to the creator of that operation.

A std::future object (let name it f) can be used to

  • wait for the result: f.wait(), f.wait_for(duration), f.wait_until(time_point)
  • retrieve the value (if ready): f.get()
  • share its state (std::shared_future): f.share(), f.valid()

More here.

62 questions
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
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
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
1 answer

Is there any potential problem in the code snippet below if `std::future::get()` would not be called?

Is there any potential problem in the code snippet below if std::future::get() would not be called? I did several tests. It seems that the said code works well without invoking std::future::get() even if it takes std::async a long time to finish its…
John
  • 2,963
  • 11
  • 33
0
votes
0 answers

Why the packaged task is terminating after throwing system error?

I am unsure why below code is throwing a runtime exception when run in online compiler here. I am trying to execute a function via packaged task #include #include int display(int i) { std::cout << "In display"; return i…
Helena
  • 444
  • 2
  • 15
0
votes
3 answers

std::future in simple words?

I saw the uses of std::future in the program written in C++. So, I quickly went to lookup what is is: std::future and got a quite complicated answer for me. Can someone put it in simple words for a 6 years old kid? I have an understanding level of a…
DonBaka
  • 325
  • 2
  • 14
0
votes
1 answer

Not all std::packaged_tasks executed when inside std::async calls

I have a rather complex code with std::async calls and std::packaged_task, that fails to execute to the very end. I simplified it to the minimal reproducable example. Two async functions are called one after another, inside of which there are…
rightaway717
  • 2,631
  • 3
  • 29
  • 43
0
votes
0 answers

Why am I getting a "read access violation" exception thrown while accessing value from std::future?

Edit: my question is different from the suggested question becaude I cannot poll 100's of future.get() and wait to execute the remaining program in main() thread. I am hoping the threads to return the value when the thread is done executing and…
The White Cloud
  • 189
  • 1
  • 11
0
votes
0 answers

Gracefully exit a program containing std::async C++

I have the main thread, which calls a function using std:async. void fileIO() { while (true) { SomeClass::someStaticFunction(); } } int main() { ... ... std::future fileIOFuture = async(std::launch::async, fileIO); …
coda
  • 2,188
  • 2
  • 22
  • 26
0
votes
1 answer

Why am I Getting error in async, future in C++

I am using std::async and std::future for the first time and am getting the error, Can somone help me rectify it. The error: ** terminate called after throwing an instance of 'std::future_error' what(): std::future_error: No associated state** The…
0
votes
1 answer

Can I change a std::sync(std::launch::deferred policy to std::launch::async, once created?

I'm in a scenario where I have many I/O-bound tasks, nicely suited to run in background. However, I can't launch all of them in parallel. Although I could use other tricks (eg. count semaphores), I wonder if I could change an already created…
olepinto
  • 351
  • 3
  • 8
0
votes
1 answer

How stop the first future if there is an exception in the second

I have a code like: std::vector> futures; for(...) { futures.emplace_back(std::async(std::launch::async, ...)) } for (auto& future : futures) { try { result += future.get(); } catch (std::exception const&…
w00drow
  • 468
  • 2
  • 11
0
votes
1 answer

Strange behaviour when using std::async with std::launch::async

I am trying to wrap my head around std::async and std::futures introduced in C++11. #include #include #include #include #include #include #include #include #include…
liv2hak
  • 14,472
  • 53
  • 157
  • 270
0
votes
2 answers

How to instantiate public members of a class and return it as a std::promise?

I wish to instantiate public members of a class and return it as a promise. This is what I am trying to do: class A { public: int x; }; std::future returnPromiseA(int y) { std::promise promise; promise.x = y; //<- THIS IS INCORECT …
JayHawk
  • 275
  • 1
  • 5
  • 15
0
votes
1 answer

Make zmqpp::socket::connect a std::future

I want to bind an overloaded function to make an std::future zmqpp::socket::connect is created like that: void connect (endpoint_t const &endpoint) The first thing i did was that: auto binded_connect = std::bind(&zmqpp::socket::connect, socket,…
lplanch
  • 45
  • 4