Reference documentation for deal.II version 9.4.1
|
#include <deal.II/base/thread_management.h>
Classes | |
class | TaskData |
Public Member Functions | |
Task (const std::function< RT()> &function_object) | |
Task ()=default | |
void | join () const |
bool | joinable () const |
internal::return_value< RT >::reference_type | return_value () |
Static Public Member Functions | |
static ::ExceptionBase & | ExcNoTask () |
Private Attributes | |
std::shared_ptr< TaskData > | task_data |
This class describes a task object, i.e., what one obtains by calling Threads::new_task(). The idea is that Threads::new_task() allows one to run a function whenever the C++ run-time system finds it convenient – typically, when there is an idle processor available. This can be used to run things in the background when there is no immediate need for the result, or if there are other things that could well be done in parallel. Whenever the result of that background task is needed, one can call either join() to just wait for the task to finish, or return_value() to obtain the value that was returned by the function that was run on that background task.
This class is conceptually similar to the std::future
class that is returned by std::async
(which is itself similar to what Threads::new_task() does). The principal conceptual difference is that one can only call std::future::get()
once, whereas one can call Threads::Task::return_value() as many times as desired. It is, thus, comparable to the std::shared_future
class. However, std::shared_future
can not be used for types that can not be copied – a particular restriction for std::unique_ptr
, for example.
Definition at line 1018 of file thread_management.h.
|
inline |
Construct a task object, given a function object to execute on the task, and then schedule this function for execution. However, when MultithreadInfo::n_threads() returns 1, i.e., if the deal.II runtime system has been configured to only use one thread, then just execute the given function object.
Definition at line 1032 of file thread_management.h.
|
default |
Default constructor. You can't do much with a task object constructed this way, except for assigning it a task object that holds data created by the Threads::new_task() functions.
|
inline |
Join the task represented by this object, i.e. wait for it to finish.
A task can be joined multiple times (while the first join() operation may block until the task has completed running, all successive attempts to join will return immediately).
If the operation that was executed on the task with which this object was initialized throws an exception instead of returning regularly, then calling the current join() function will first wait for that task to finish, and then in turn throw the exception that the task operation had thrown originally. This allows for the propagation of exceptions from tasks executed on a separate thread to the calling thread.
(This behavior differs from that of std::future
, where the std::future::wait()
function only waits for completion of the operation, whereas the exception is propagated only once one calls std::future::get()
. However, this is awkward when putting void
functions onto separate tasks because these do not actually return anything; consequently, it is more natural to call std::task::wait()
for such tasks than the std::task::get()
function since the latter does not, actually, return anything that could be gotten.)
Definition at line 1203 of file thread_management.h.
|
inline |
Return whether the current object can be joined. You can join a task object once a task (typically created with Threads::new_task()) has actually been assigned to it. On the other hand, the function returns false if the object has been default constructed.
A task can be joined multiple times (while the first join() operation may block until the task has completed running, all successive attempts to join will return immediately). Consequently, if this function returns true, it will continue to return true until the task object it reports on is assigned to from another object.
Definition at line 1224 of file thread_management.h.
|
inline |
Get the return value of the function of the task. Since it is only available once the thread finishes, this function internally also calls join(). You can call this function multiple times as long as the object refers to the same task, and expect to get the same return value every time. (With the exception of the case where the returned object has been moved; see below.)
const
reference to the returned object, instead of the returned object. This allows writing code such as const
) reference to support code such as this: std::move
the returned object (namely, the std::unique_ptr
object) because std::unique_ptr
objects can not be copied. In other words, to get the pointer out of the object returned from the task, it needs to be moved, and in order to be moved, the current function needs to return a writable (non-const
) reference.This function internally calls the join() member function. As a consequence, and as explained there, if the packaged task throws an exception that is then re-thrown by the join() function and consequently also the current function if you have not previously called join().
Definition at line 1280 of file thread_management.h.