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

Why does only some of my objects get created using std::async

I have a loop that pushes back calls of std::async that are used to create objects in the pointed function and emplace them back to another vector. All the calls are pushed to the futures function and the results are ready when i used the VS…
Chill Kid
  • 11
  • 4
0
votes
3 answers

Using std::async slower than non-async method to populate a vector

I am experimenting with std::async to populate a vector. The idea behind it is to use multi-threading to save time. However, running some benchmark tests I find that my non-async method is faster! #include #include #include…
jignatius
  • 6,304
  • 2
  • 15
  • 30
0
votes
1 answer

How can I pass this std::function to std::async

I am working on a piece of multi threaded code but I can't seem to pass a std::function object to the std::async function. I'm sure I am doing something wrong but I can't figure out what that would be. Therefore, I prepared this piece of code so…
MaestroMaus
  • 342
  • 1
  • 6
  • 18
0
votes
1 answer

How do I declare an async function?

I am new to threads and async functions and I am trying use async function to return information from the function below: std::vector > calculatingSlices(SquareMatrix A, std::vector > slices) and I am doing this…
0
votes
1 answer

Lifetime of local variables inside a std::async lambda function

In the following snippet: std::future result = std::async(std::launch::async, []() { std::vector someLocalVariable{GottenFromSomewhere()}; return SomeReallyLongLastingProcessingPipeline(someLocalVariable); }); I am inclined to…
theodor96
  • 41
  • 8
0
votes
1 answer

Member std::future preventing boost::shared_ptr from going out of scope

I have a function that creates a new bridge object and stores it as a boost::shared_ptr: bool proxy::bridge::acceptor::accept_connections() { try { session_ = boost::shared_ptr(new bridge(io_service_)); …
0
votes
2 answers

scope block when use std::async in function other than the main function

I have some problem with st::async when is use this in other function other than Main function, suppose, I have functions like flowing : void printData() { for (size_t i = 0; i < 5; i++) { std::cout << "Test Function" << std::endl; …
0
votes
2 answers

Tasks on asio::strand are running on a single thread

I modified an asio strand example using the standalone version of the library from 4a here #include #include #include #include #include #include using namespace…
r0n9
  • 2,505
  • 1
  • 29
  • 43
0
votes
2 answers

std::launch::async does the number of distinct thread is correct?

I was reading abut std::async with std::launch::async and read that with that policy, the callable will be called in a new thread. So for sake of test, I did as follow: struct Wrapper { void consume() { std::cout…
Blood-HaZaRd
  • 2,049
  • 2
  • 20
  • 43
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
0 answers

C++ - Running functions using async causes random blocking

I am creating a worker process which waits for IDs on a pipe and against each ID tries to fetch some data. This fetch could take a while so I also create an async task to handle the time out logic. The problem is if I send it multiple IDs through…
user3690467
  • 3,049
  • 6
  • 27
  • 54
0
votes
0 answers

Calling std::async function inside a synchronous function in C++

Consider following examples: 1. All functions are called asynchronously void threadFunc() { while (1) { std::cout << "thread working..\n"; std::this_thread::sleep_for(1s); } } void middleFunc() { …
0
votes
1 answer

Wake a deferred task object without invoking future.get()

What happens when you call std::future::wait_for on a deferred task object? Ideally I would like to wake a deferred task but not take the hit of processing the task in the current thread.
Ram
  • 3,045
  • 3
  • 27
  • 42
0
votes
1 answer

Using c++11's std::async inside an abstract base class

Why doesn't making threads like this work inside of an abstract base class? I'm trying to abstract away all of the multithreading details for users who derive from this base class. I don't understand why it says "no type named 'type'" when I…
Taylor
  • 1,797
  • 4
  • 26
  • 51
0
votes
1 answer

How long is std::async running?

If I create an std::async object in a class, how long does the corresponding thread run? Until the desctructor of the containing class (Bar) is called? class Bar { public: Bar() { handle = std::async( std::launch::async, …
user695652
  • 4,105
  • 7
  • 40
  • 58
1 2 3
10
11