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
1
vote
2 answers

How to use a range-based for loop with a vector of future

I have a program that calculates some values in different threads with std::packaged_task. I store the std::future I get from the packaged tasks via get_future() in a vector (defined as std::vector>). When I calculate the sum…
ByteNudger
  • 1,545
  • 5
  • 29
  • 37
1
vote
1 answer

Is there a way to write generic code for any function such that it can be executed (asyncronously) and a return value obtained from a thread pool?

I have been trying to create a thread pool class(for personal experimentation/use/fun). I found a way to accept any function with any arguments/return type by using parameter packs (seen in code below) and binding the functions to std::function.…
Paul
  • 11
  • 1
1
vote
0 answers

nvcc compilation error in code with included future header

After reinstaling VS2015 to have version of with Update 3 on my Windows 7 dell laptop I have started to have problem with compilation of cuda projects with future header: #include "cuda_runtime.h" #include #include int…
1
vote
2 answers

Segmentation fault because of moved promise

I've passed a promise as a reference to a thread. Afterwards, the promise was moved into a vector via std::move. This is causing a segmentation fault when the software is executed. I reckon the reference in the thread is never updated after moving…
Gizmo
  • 871
  • 1
  • 15
  • 38
1
vote
1 answer

Can a std::promise know that the respective std::future has cancelled waiting?

I'm in a situation where I have a continuous thread working on some input. However, sometimes the work load is too high and the corresponding future will not wait for the result. I need to free some resources in that case, since the computation…
DomTomCat
  • 8,189
  • 1
  • 49
  • 64
1
vote
1 answer

std::Future_error when using std::promise

I'm trying to make a video player. I have added a thread to time how long a video should be show on the screen. I'm trying to decode the video and update window in the main thread; the second thread will get the packets, see how long the packet…
Gathros
  • 31
  • 2
  • 6
1
vote
1 answer

Lifetime issues of std::promise in an async API

I'm wondering how to develop an asynchronous API using promises and futures. The application is using a single data stream that is used for both unsolicited periodic data and requesty/reply communication. For the requesty/reply blocking until the…
PeppeDx
  • 133
  • 1
  • 10
0
votes
0 answers

Difference between directly getting a std::shared_future from std::promise and converting a std::future to std::shared_future?

I've been exploring C++'s concurrency features, specifically around futures and promises. I came across two different ways to obtain a std::shared_future, and I'm trying to understand if there's any significant difference between them. Method 1:…
Sami
  • 513
  • 4
  • 11
0
votes
0 answers

How to check if a std::future is empty

I am building a map generator in C++ using glm, perlin, and std::future to generate chunks of a map. But for some reason, after some time, I get an empty future. I've tried to find some info about what does. It means if a future is empty and how to…
Marko Taht
  • 1,448
  • 1
  • 20
  • 40
0
votes
1 answer

Multithreading with std::future in C++: Accessing shared data

I am currently developing a multi-threaded application in C++ where different threads are expected to process data from a shared data structure. I'm aware that the standard library provides std::future and std::async to easily handle asynchronous…
Ravement
  • 13
  • 6
0
votes
0 answers

Calling async thread from pythonBinding

I have a python based application and it calls the below .cpp code through pythonBinding. while(1) { auto statusResponse = _mProxy->GetStatusAsync( nodeKey, std::bind(&NodeIfaceImpl::getStatusDbusCallback, this, …
deepan muthusamy
  • 321
  • 2
  • 14
0
votes
1 answer

Async Task Execution and Future Object Requirement for C++ Void Functions: Understanding the Need for std::future Objects in C++Async

In the first example code, all tasks are launched successfully without any issues. However, in the second example code, only the first task is launched, and the program waits there without executing the remaining lines of code. It seems even if when…
Sami
  • 513
  • 4
  • 11
0
votes
2 answers

Why the below program aborts when a std::vector> is used.?

I wanted to perform hashing of a stream of input messages in multithreading, so was trying to implement std::vector> futures; but the program aborts from abort.h when debugging in Visual Studio 2019. Code…
devilsEye
  • 1
  • 1
0
votes
1 answer

How many std::future objects can exist in a system at a time simultaneously.?

I wanted to perform hashing of a stream of input messages in multithreading, so was trying to implement std::vector> futures; but not sure as how many future objects can exist in a system, at a time…
devilsEye
  • 1
  • 1
0
votes
0 answers

What's the behavior of std::future when libcpr::PostCallback got Timeout/ConnectionTimeout?

For certain reasons I'm trying to use libcpr::PostCallback to send requests, and a customized callback function to parse responses for later use. Since libcpr::PostCallback use std::future as its return value, so I implemented a thread-safe list to…
starskyTW
  • 21
  • 2