Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can a function run "as if" on a new thread without doing so?

Per [futures.async]/3 bullet 1 of the C++ Standard, when a function f is passed to std::async with the std::launch::async launch policy, f will run "as if in a new thread of execution".

Given that f can do anything, including loop infinitely and block forever, how can an implementation offer the behavior of f running on its own thread without actually running it on its own thread? That is, how can an implementation take advantage of the "as if" wiggle room the Standard provides?

like image 770
KnowItAllWannabe Avatar asked May 07 '18 19:05

KnowItAllWannabe


People also ask

How do you call a function in one thread from another?

There's no way to do it explicitly. You can transfer messages between your threads, e.g. using events, which would trigger calling of the method needed. But the target thread must be in loop, waiting for message. It's the only one way you can force the thread to go in another subroutin.

How do you run a function in a separate thread C++?

To start a thread we simply need to create a new thread object and pass the executing code to be called (i.e, a callable object) into the constructor of the object. Once the object is created a new thread is launched which will execute the code specified in callable. After defining callable, pass it to the constructor.

What does std :: thread do?

std::thread Threads allow multiple functions to execute concurrently. std::thread objects may also be in the state that does not represent any thread (after default construction, move from, detach, or join), and a thread of execution may not be associated with any thread objects (after detach).

What is STD future?

The class template std::future provides a mechanism to access the result of asynchronous operations: An asynchronous operation (created via std::async, std::packaged_task, or std::promise) can provide a std::future object to the creator of that asynchronous operation.


2 Answers

Looking in the C++ refs as appear here and here, it seems that the "as if" goal is to let the library implementer some degree of freedom. For example, it says

as if spawned by std::thread(std::forward(f), std::forward(args)...), except that if the function f returns a value or throws an exception, it is stored in the shared state accessible through the std::future that async returns to the caller.

in the first source, and

as if a thread object is constructed with fn and args as arguments, and accessing the shared state of the returned future joins it

in the second. So it looks like the behavior is similar to that of std::thread but might be implemented in a different way. This is interesting, since the phrase thread of execution you quote here is distinguishable from std::thread. Still, it seems that both sources understand the as-if that way.

Another option might be, as suggested by François Andrieux to allow usage of threadpool, as expressed in the first source:

The template function async runs the function f asynchronously (potentially in a separate thread which may be part of a thread pool)

like image 179
Mike Avatar answered Oct 12 '22 11:10

Mike


The advantage of this solution is when you have optimized implementation for specific role in multi threading world. As I am involved in software update procedures I will use such example. For optimization reason you can call:

std::thread::hardware_concurrency();

You will receive:

Returns the number of concurrent threads supported by the implementation.

Let's say you have result equal 4. You want to perform update many things in parallel. Main thread is monitoring list of futures(3 left) and checking from time to time is it done or not and if done execute next thing from ToDo list. Profit here is for example if you are updating different type of memory like 1-FileSystem, 2-EEPROM, 3-NOR memory or something like that. There is no profit of checking in loop without delay, so you want to give a task to 4-th thread between checks. You are writing function that is digging bitcoins for 50 miliseconds :) and you trigger it as deferred between checks.

And then as you mentioned we have:

advantage of the "as if" wiggle room the Standard provides

like image 25
Marek Adam Niemiec Avatar answered Oct 12 '22 13:10

Marek Adam Niemiec