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
1
vote
1 answer

std::async blocks even with std::launch::async flag depending on whether the returned future is used or ignored

Description of the problem std::async seems to block even with std::launch::async flag: #include #include #include int main(void) { using namespace std::chrono_literals; auto f = [](const char* s) { …
Young-hwi
  • 607
  • 7
  • 14
1
vote
1 answer

Inconsistency in performance of empty kernel with multi threads

Below code is to check the performance of the empty kernels (to verify the dispatch rate of the kernel) with multi threads using std async. #include #include #include #include #include #include…
Satyanvesh D
  • 323
  • 1
  • 4
  • 16
1
vote
0 answers

Slurm only using all CPUs on some software when using srun?

I have a script defined like this: #!/bin/sh #SBATCH --nodes=1 #SBATCH --cpus-per-task=16 #SBATCH --mem 180000 ./program1 --threads 16 ./program2 --threads 16 I then submit my job with sbatch job.sh The thing is that program1 uses all 16…
Ivan
  • 1,801
  • 2
  • 23
  • 40
1
vote
1 answer

How to show Cdialog with std::async

I have a derive class named A was inheritance from CDialog, I created the object to A named a and want to utilize the member function domodal to show dialog. Nonetheless, this dialog cannot show and parent window was block. A a(this); auto…
Savner_Dig
  • 21
  • 1
  • 8
1
vote
1 answer

Execute sql queries asynchronously in C++

I have an application that reads data into a map and writes them to a sqlite database. The application does a lot of other things and is asynchronous (uses boost asio) except for writing to db part. Here is the sample pseudo code: struct…
psy
  • 914
  • 3
  • 10
  • 31
1
vote
1 answer

Is it possible to cancel a blocking call in C++ 11 or 14?

My gcc compiler supports C++ 14. Scenario: I want to know if there is a way I can force cancel out of a blocking call and stop my std::thread safely. Code: // Member vars declared in MyClass.hpp std::atomic m_continue_polling =…
TheWaterProgrammer
  • 7,055
  • 12
  • 70
  • 159
1
vote
2 answers

How to use std::async efficiently to perform operations on pointer array

I am very new to modern C++ library, and trying to learn how to use std::async to perform some operations on a big pointer array. The sample code I have written is crashing at the point where the async task is launched. Sample code: #include…
1
vote
1 answer

std::atomic and lambda

Does anybody know why this program goes into an infinite loop, instead of stopping after 5s or so? This happens with both the latest gcc and clang compiler; does atomic_bool suffer from hte same issues as a vector of bool? If I use atomic this…
user8063157
  • 285
  • 1
  • 13
1
vote
1 answer

Why is VC++ matrix times vector faster with openMP than async?

I coded multiplication of a vector by a matrix two ways, one with openMP, the other with std::async. I expected the performance to be virtually identical. OpenMP is slow on the first call, probably because it defers making a thread pool. After that,…
Jive Dadson
  • 16,680
  • 9
  • 52
  • 65
1
vote
1 answer

What is causing data race in std::async here?

I recently created a pattern searching program in Conway's Game of Life, but It ran too slow to be practical. So I decided to parallelize it, but I failed; it caused segmentation fault, which is very likely due to data race. A brief explanation of…
Dannyu NDos
  • 2,458
  • 13
  • 32
1
vote
0 answers

std::async with std::future_status::timeout, times out every alternate time on iOS only

I am using an std::async in the C++ part of my app to do a heavy transaction which has chances of taking a very long time in case of something wrong with the network. So I use an std::async combined with std::future waiting for a time out set which…
TheWaterProgrammer
  • 7,055
  • 12
  • 70
  • 159
1
vote
1 answer

Using std::async in constructor

I am quite new to the C++11 feature std::async and I fail to grasp why the code below never prints bar. Could someone shed some light on this for me? class Thready { public: Thready() { std::async(std::launch::async, &Thready::foo,…
user695652
  • 4,105
  • 7
  • 40
  • 58
1
vote
1 answer

Using std::bind to capture a parameter pack "by move"

I'm attempting to implement std::async from scratch, and have run into a hiccup with arguments of move-only type. The gist of it is, C++14 init-captures allow us to capture single variables "by move" or "by perfect forwarding", but they do not…
Quuxplusone
  • 23,928
  • 8
  • 94
  • 159
1
vote
0 answers

C++: VS IntelliSense and the return value of std::async

I have a piece of code which uses std::async. This is the function I call via async: cv::Mat CtVolume::prepareProjection(size_t index, FilterType filterType) const As one can see, it's return value is cv::Mat. Now somewhere in my code I declare a…
bweber
  • 3,772
  • 3
  • 32
  • 57
1
vote
2 answers

C++11 std::async Callback

I would like to implement an object that queries a database and executes a query asynchronously. Suppose we have an object A to query uses the object B. A calls executeQuery method of B and starts to do anything else. ExecuteQuery method of B is…
DaxDeveloper
  • 138
  • 1
  • 13