Public Member Functions | Public Attributes | Static Private Member Functions

Threads::internal::ThreadDescriptor< RT > Struct Template Reference

Inheritance diagram for Threads::internal::ThreadDescriptor< RT >:
Inheritance graph
[legend]

List of all members.

Public Member Functions

 ThreadDescriptor ()
void start (const std_cxx1x::function< RT()> &function)
void join ()

Public Attributes

std_cxx1x::shared_ptr
< ThreadDescriptor< RT > > 
self
std_cxx1x::thread thread
return_value< RT > ret_val
bool thread_is_active

Static Private Member Functions

static void thread_entry_point (const std_cxx1x::function< RT()> function, ThreadDescriptor< RT > *descriptor)

Detailed Description

template<typename RT>
struct Threads::internal::ThreadDescriptor< RT >

A class that represents threads. For each thread, we create exactly one of these objects -- exactly one because it carries the returned value of the function called on the thread.

While we have only one of these objects per thread, several Threads::Thread objects may refer to this descriptor.

Note, however, that since this object holds the location where we store the return value of the thread, the object has to live at least as long as the thread executes. This means that even if all Threads::Thread objects that refered to this descriptor (through a std::shared_ptr) have gone out of scope, we must still hold on to the object. We do this by having the descriptor keep a pointer to itself and reset it to zero once it is done -- effectly keeping the use pointer above zero as long as work is going on.

To enable the current class to obtain a shared_ptr from itself, we derive from the class std::enable_shared_from_this.

Definition at line 1273 of file thread_management.h.


Constructor & Destructor Documentation

template<typename RT>
Threads::internal::ThreadDescriptor< RT >::ThreadDescriptor (  ) [inline]

Default constructor.

Definition at line 1352 of file thread_management.h.


Member Function Documentation

template<typename RT>
void Threads::internal::ThreadDescriptor< RT >::start ( const std_cxx1x::function< RT()> &  function ) [inline]

Start the thread and let it put its return value into the ret_val object.

Note that we cannot already do this in the constructor of this class: we will call enable_shared_from_this::shared_from_this which requires that there is already a shared_ptr that owns this object; however, at the time of creation, there can of course be no shared_ptr that owns the object -- it is, after all, just being created and can not already have been handed off to a shared_ptr; as a consequence, we first create the object, give it to a shared_ptr, and then call shared_from_this.

Definition at line 1384 of file thread_management.h.

References Threads::internal::ThreadDescriptor< RT >::thread, Threads::internal::ThreadDescriptor< RT >::thread_entry_point(), and Threads::internal::ThreadDescriptor< RT >::thread_is_active.

template<typename RT>
void Threads::internal::ThreadDescriptor< RT >::join (  ) [inline]
template<typename RT>
static void Threads::internal::ThreadDescriptor< RT >::thread_entry_point ( const std_cxx1x::function< RT()>  function,
ThreadDescriptor< RT > *  descriptor 
) [inline, static, private]

Member Data Documentation

template<typename RT>
std_cxx1x::shared_ptr<ThreadDescriptor<RT> > Threads::internal::ThreadDescriptor< RT >::self

A pointer to the current object, kept nonzero as long as the thread is executing to avoid destroying the current object while we are still expecting to write something into the return value location.

Definition at line 1286 of file thread_management.h.

Referenced by Threads::internal::ThreadDescriptor< RT >::thread_entry_point().

template<typename RT>
std_cxx1x::thread Threads::internal::ThreadDescriptor< RT >::thread

An object that represents the thread started.

Definition at line 1292 of file thread_management.h.

Referenced by Threads::internal::ThreadDescriptor< RT >::join(), and Threads::internal::ThreadDescriptor< RT >::start().

template<typename RT>
return_value<RT> Threads::internal::ThreadDescriptor< RT >::ret_val

An object that will hold the value returned by the function called on the thread.

Definition at line 1299 of file thread_management.h.

Referenced by Threads::internal::ThreadDescriptor< RT >::thread_entry_point().

A bool variable that is initially false, is set to true when a new thread is started, and is set back to false once join() has been called.

We use this variable to make sure we can call join() twice on the same thread. For some reason, the C++ standard library throws a std::system_error exception if one tries to call std::thread::join twice (and in fact, before the second call, std::thread::joinable returns false) but this is a somewhat desirable thing to do because one doesn't have to keep track whether join() has been called before. Using this variable, whenever we have called join() before, the variable is set to true and we can skip over calling std::thread::join() a second time.

Further, note that we need no mutex for this variable: threads can only be join from the thread that created it originally. Consequently, everything that happens in a function that does not create threads (such as the join() function below) looks atomic to the outside world. Since we clear and test thread_is_active in the same function as we call std::thread::join, these actions are atomic and need no mutex. Of course, two threads may call join() on the same thread object at the same time, but this action is undefined anyway since they can not both join the same thread.

Definition at line 1347 of file thread_management.h.

Referenced by Threads::internal::ThreadDescriptor< RT >::join(), and Threads::internal::ThreadDescriptor< RT >::start().


The documentation for this struct was generated from the following file:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

deal.II documentation generated on Fri Feb 3 2012 06:04:17 by doxygen 1.7.2