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
0 answers

Why does my C++ function, 'cannot determine which instance of overloaded function is intended'?

I am launching a std::async thread as follows; cv::Point2f opDim = Point2f(1920, 1080); cv::Point2f ipDim = Point2f(1920, 1080); CameraToBEV cbevObj(roiBox1, opDim, ipDim); std::vector cbevVec; for (int j = 0; j <…
The White Cloud
  • 189
  • 1
  • 11
2
votes
1 answer

How to use std future and async with threading in a for loop with a shared resource as param?

I got stuck with an implementation problem in my threading practice project. I know what I want to achieve but I dont know how. I am new in the topic of std::future and std::async so I am not even sure if the implementation is even possible in the…
Seraph
  • 43
  • 4
2
votes
1 answer

Do you need to store the std::future return value from std::async?

Consider the follow code: #include #include #include #include void func() { std::async(std::launch::async, []{std::this_thread::sleep_for(std::chrono::milliseconds(1000)); }); } int main() { std::cout…
code_fodder
  • 15,263
  • 17
  • 90
  • 167
2
votes
1 answer

How std::async works: why it invokes copy/move so many times?

Basically the title #include #include class A { public: A () {}; A (const A& a) {std::cout<<"A copy\n";}; A (A&& a) noexcept {std::cout<<"A move\n";}; ~A () {std::cout<<"A dest\n";}; void fun () {…
DoehJohn
  • 201
  • 1
  • 8
2
votes
1 answer

c++ std::async slower then sequential for loop

I am trying to create a physics engine for a custom game engine. At the moment everything works fine however i am having some performance issues when the engine has to deal with approximately 4000 physics bodies. I am quite certain this is not the…
2
votes
1 answer

Using std::async and pass arguments in a vector to a function and gather results

I want to make this function to compute in parallel: #include #include int compute_something(int i, int j) { return i*j; } int main() { auto params = std::vector(1000,5); std::vector results; for…
Jake B.
  • 435
  • 3
  • 13
2
votes
3 answers

Why can't `std::async` pick the correct overload?

Please consider the following example: #include #include std::size_t calc_something(std::size_t lim_) { std::size_t result = lim_ * 10; return result; } void calc_something(std::size_t lim_, std::promise
Constantinos Glynos
  • 2,952
  • 2
  • 14
  • 32
2
votes
0 answers

Cross-compilation errors with std::async

Background I built a toolchain for my ARMv7 Raspberry Pi using crosstool-ng on my Ubuntu 18.04. Due to a problem with GCC 7.1 compilation of GCC 6.3.0 I did some modifications to ubsan.c file. I built the required libraries on the Raspberry Pi…
2
votes
2 answers

C++ errors with Variadic template

I have the following code using variadic templates to call std::async, struct TestParent { template< typename Fn, typeName ...Args > bool checkBlock( bool& toCheck, Fn&& fn, Args&& ... args ) { int startIndx…
mark
  • 89
  • 1
  • 6
2
votes
0 answers

C++ std::future hangs on timeout

All I have setup a test project for learning std::future and its use with std::async. It is a simple console app and the code is as follows #include #include #include using namespace std; std::string getLine() { …
sramalingam24
  • 1,297
  • 1
  • 14
  • 19
2
votes
1 answer

C++ - display progress bar when using std::async

So I'm working on a ray-tracer, and in order to reduce rendering time, I used std::async to do pixel calculations independently. I used this tutorial, and everything works great, and indeed I was able to save about 70% in rendering time. Still,…
Alonbs
  • 259
  • 1
  • 11
2
votes
1 answer

std::async with gcc 7.2 need pthread link option

The following code need pthread link option to compile, and i don't understand why. Do you have any idea? I'm using gcc 7.2.0 #include int sum = 0; void func() { for(int i=0; i < 10; ++i) sum +=i; } int main() { …
bonpiedlaroute
  • 173
  • 1
  • 9
2
votes
2 answers

use_count() of shared_ptr moved into a std::async in gcc 4.6.3

In the code below, I want the use_count() of the shared_ptr moved into the std::async to be 1: #include #include #include using namespace std; void fun(shared_ptr sp) { cout << "fun: sp.use_count() == " <<…
Quokka
  • 174
  • 1
  • 3
  • 10
2
votes
1 answer

How to prematurely kill std::async threads before they are finished *without* using a std::atomic_bool?

I have a function that takes a callback, and used it to do work on 10 separate threads. However, it is often the case that not all of the work is needed. For example, if the desired result is obtained on the third thread, it should stop all work…
Taylor
  • 1,797
  • 4
  • 26
  • 51
2
votes
0 answers

Exceptions from boost::async() lose type

Boost version: 1.61, GCC version 6.2.0, C++14 Exceptions inside a boost::async() call seem to lose their type, while it works correctly with std::async. Is there a fundamental difference between std::async and boost::async I'm missing? The following…
Heinzi
  • 5,793
  • 4
  • 40
  • 69