Utility class to manipulate threads. More...
#include <Thread.hpp>
Public Types | |
typedef void(* | FuncType )(void *) |
Public Member Functions | |
Thread (FuncType function, void *userData=NULL) | |
Construct the thread from a function pointer. | |
virtual | ~Thread () |
Virtual destructor. | |
void | Launch () |
Run the thread. | |
void | Wait () |
Wait until the thread finishes. | |
void | Terminate () |
Terminate the thread. | |
Protected Member Functions | |
Thread () | |
Default constructor. |
Utility class to manipulate threads.
Threads provide a way to run multiple parts of the code in parallel.
When you launch a new thread, the execution is split and both the new thread and the caller run in parallel.
There are two ways to use sf::Thread. The first (and preferred) way of using it is to created derived classes. When inheriting from sf::Thread, the virtual function Run() has to be overriden, and defines the entry point of the thread. The thread automatically stops when this function ends.
Usage example:
class MyThread : public sf::Thread { private : virtual void Run() { // beginning of the thread ... // end of the thread } }; MyThread mythread; mythread.Launch(); // start the thread (internally calls thread.Run()) // from now on, both mythread and the main thread run in parallel
The second way of using sf::Thread uses a non-member function as the entry point and thus doesn't involve creating a new class. However, this method is only provided for compatibility with C-style code or for fast prototyping; you are strongly encouraged to use the first method.
Usage example:
void ThreadFunc(void* data) { // beginning of the thread ... // end of the thread } sf::Thread mythread(&ThreadFunc, NULL); mythread.Launch(); // start the thread (internally calls ThreadFunc(NULL)) // from now on, both mythread and the main thread run in parallel
Creating parallel threads of execution can be dangerous: all threads inside the same process share the same memory space, which means that you may end up accessing the same variable from multiple threads at the same time. To prevent this kind of situations, you can use mutexes (see sf::Mutex).
Definition at line 47 of file Thread.hpp.
sf::Thread::Thread | ( | Thread::FuncType | function, | |
void * | userData = NULL | |||
) |
Construct the thread from a function pointer.
function | Entry point of the thread | |
userData | Data to pass to the thread function |
Definition at line 55 of file Thread.cpp.
sf::Thread::~Thread | ( | ) | [virtual] |
Virtual destructor.
This destructor calls Wait(), so that the thread cannot survive when its sf::Thread instance is destroyed.
Definition at line 65 of file Thread.cpp.
sf::Thread::Thread | ( | ) | [protected] |
Default constructor.
This constructor is only accessible from derived classes
Definition at line 45 of file Thread.cpp.
void sf::Thread::Launch | ( | ) |
Run the thread.
Definition at line 72 of file Thread.cpp.
void sf::Thread::Terminate | ( | ) |
Terminate the thread.
This function immediately stops the thread, without waiting for its function to finish. Terminating a thread with this function is not safe, and can lead to local variables not being destroyed on some operating systems. You should rather try to make the thread function terminate by itself.
Definition at line 92 of file Thread.cpp.
void sf::Thread::Wait | ( | ) |
Wait until the thread finishes.
This function will block the execution until the thread's function ends.
Definition at line 80 of file Thread.cpp.