Neo  0.5.0
Developer Documentation
Thread.h
Go to the documentation of this file.
1 //========================================================================
2 // Copyright (c) 2014 Yannick Pflanzer <scary-squid.de>
3 //
4 // This software is provided 'as-is', without any express or implied
5 // warranty. In no event will the authors be held liable for any damages
6 // arising from the use of this software.
7 //
8 // Permission is granted to anyone to use this software for any purpose,
9 // including commercial applications, and to alter it and redistribute it
10 // freely, subject to the following restrictions:
11 //
12 // 1. The origin of this software must not be misrepresented; you must not
13 // claim that you wrote the original software. If you use this software
14 // in a product, an acknowledgment in the product documentation would
15 // be appreciated but is not required.
16 //
17 // 2. Altered source versions must be plainly marked as such, and must not
18 // be misrepresented as being the original software.
19 //
20 // 3. This notice may not be removed or altered from any source
21 // distribution.
22 //
23 //========================================================================
24 
25 #ifndef __THREAD_H
26 #define __THREAD_H
27 
28 namespace Neo
29 {
37 class NEO_CORE_EXPORT Thread
38 {
39 private:
40  bool m_running;
41 public:
42  Thread() {}
43 
47  ~Thread() {}
48 
64  virtual bool Start(int (*thread_func)(void*), const char* name, void* data) = 0;
65 
72  virtual void Stop() = 0;
73 
78  virtual int WaitForReturn() = 0;
79 
84  bool IsRunning() { return m_running; }
85  void SetRunning(bool value) { m_running = value; }
86 
91  virtual int GetId() = 0;
92 
97  virtual Thread* getNew() = 0;
98 };
99 
107 class NEO_CORE_EXPORT Semaphore
108 {
109 public:
112 
118  virtual bool Init(int num) = 0;
119 
126  static bool WaitAndLock(Semaphore*);
127  static bool Unlock(Semaphore*);
128 
129  virtual bool WaitAndLock() = 0;
130  virtual bool Unlock() = 0;
131 
136  virtual Semaphore* getNew() = 0;
137 
138 };
139 
144 class NEO_CORE_EXPORT ThreadFactory
145 {
146  Thread* m_templateThread;
147  Semaphore* m_templateSemaphore;
148 public:
149 
150  static ThreadFactory* getInstance();
151 
155  void clear() { SAFE_DELETE(m_templateThread); SAFE_DELETE(m_templateSemaphore); }
156 
161  void setTemplateThread(Thread* thr) { m_templateThread = thr; }
162 
167  void setTemplateSemaphore(Semaphore* sem) { m_templateSemaphore = sem; }
168 
173  Thread* getNewThread() { if(m_templateThread) return m_templateThread->getNew(); else return NULL; }
174 
179  Semaphore* getNewSemaphore() { if(m_templateSemaphore) return m_templateSemaphore->getNew(); else return NULL; }
180 };
181 }
182 #endif // MTHREAD_H
Semaphore()
Definition: Thread.h:110
The MSemaphore class implements a semaphore mechanism based on SDL for use with MThread.
Definition: Thread.h:107
void setTemplateSemaphore(Semaphore *sem)
Sets the semaphore template object with the overwritten getNew method.
Definition: Thread.h:167
virtual Thread * getNew()=0
Creates a new thread object. Should be overwritten by child classes.
~Semaphore()
Definition: Thread.h:111
Thread()
Definition: Thread.h:42
void clear()
Deletes all templates.
Definition: Thread.h:155
The MThread class implements a multithreading mechanism.
Definition: Thread.h:37
virtual Semaphore * getNew()=0
Creates a new semaphore object.
Semaphore * getNewSemaphore()
Creates a new semaphore.
Definition: Thread.h:179
void SetRunning(bool value)
Definition: Thread.h:85
Thread * getNewThread()
Creates a new thread.
Definition: Thread.h:173
~Thread()
The destructor calls MThread::Stop to clean up.
Definition: Thread.h:47
bool IsRunning()
IsRunning Returns if the thread is currently running.
Definition: Thread.h:84
Definition: Color.h:29
void setTemplateThread(Thread *thr)
Sets the thread template object with the overwritten getNew method.
Definition: Thread.h:161
The ThreadFactory class allows you to register any subclass of Thread and Semaphore to provide the im...
Definition: Thread.h:144