Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/terrain.older/src/lib/util/threading.h @ 10761

Last change on this file since 10761 was 9140, checked in by bensch, 18 years ago

merged back

File size: 1.4 KB
Line 
1/*!
2 * @file threading.h
3 * @brief Definition of Thread Classes.
4 *
5 * These are mainly Classes, that are used for wrapping around SDL_thread
6*/
7
8#ifndef _THREADING_H
9#define _THREADING_H
10
11#ifdef HAVE_SDL_H
12 #include <SDL_thread.h>
13#else
14 #include <SDL/SDL_thread.h>
15#endif
16#include <string>
17namespace OrxThread
18{
19  //! A class for Wrapping Threads
20  class Thread
21  {
22
23  public:
24    Thread(int (*fn)(void *), void *data) { this->thread = SDL_CreateThread(fn, data); };
25    virtual ~Thread() { SDL_KillThread(this->thread); }
26    void exit ( int returnCode = 0 );
27    bool isFinished () const;
28    bool isRunning () const;
29    void wait() { SDL_WaitThread(this->thread, NULL); };
30
31    void start();
32    void terminate();
33  private:
34    SDL_Thread* thread;
35
36  };
37
38  class Mutex
39  {
40  public:
41    Mutex() {  this->mutex = SDL_CreateMutex(); };
42    ~Mutex() { SDL_DestroyMutex(this->mutex); }
43
44    void lock() { SDL_mutexP(mutex); };
45    void unlock() { SDL_mutexV(mutex); };
46
47    SDL_mutex* getMutex() const { return this->mutex; };
48  private:
49    SDL_mutex* mutex;
50  };
51
52  //! A Class that locks a Mutex within its scope
53  class MutexLock
54  {
55  public:
56    //! Locks the Mutex mutex in this Scope.
57    MutexLock(Mutex* mutex) { SDL_mutexP(mutex->getMutex()); this->mutex = mutex; };
58    ~MutexLock() { SDL_mutexV(mutex->getMutex()); };
59  private:
60    Mutex* mutex;         //!< The Mutex to lock.
61  };
62}
63
64#endif /* _THREADING_H */
Note: See TracBrowser for help on using the repository browser.