
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) |
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.
| Threads::internal::ThreadDescriptor< RT >::ThreadDescriptor | ( | ) | [inline] |
Default constructor.
Definition at line 1352 of file thread_management.h.
| 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.
| void Threads::internal::ThreadDescriptor< RT >::join | ( | ) | [inline] |
Wait for the thread to end.
Definition at line 1398 of file thread_management.h.
References Assert, StandardExceptions::ExcInternalError(), Threads::internal::ThreadDescriptor< RT >::thread, and Threads::internal::ThreadDescriptor< RT >::thread_is_active.
| static void Threads::internal::ThreadDescriptor< RT >::thread_entry_point | ( | const std_cxx1x::function< RT()> | function, |
| ThreadDescriptor< RT > * | descriptor | ||
| ) | [inline, static, private] |
The function that runs on the thread.
Definition at line 1418 of file thread_management.h.
References Assert, Threads::internal::call(), StandardExceptions::ExcInternalError(), Threads::internal::ThreadDescriptor< RT >::ret_val, and Threads::internal::ThreadDescriptor< RT >::self.
Referenced by Threads::internal::ThreadDescriptor< RT >::start().
| 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().
| 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().
| 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().
| bool Threads::internal::ThreadDescriptor< RT >::thread_is_active |
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().
documentation generated on Fri Feb 3 2012 06:04:17 by
doxygen
1.7.2