ZenLib
Thread.h
Go to the documentation of this file.
1 // ZenLib::Thread - Thread functions
2 // Copyright (C) 2007-2011 MediaArea.net SARL, Info@MediaArea.net
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 be
15 // appreciated but is not required.
16 // 2. Altered source versions must be plainly marked as such, and must not be
17 // misrepresented as being the original software.
18 // 3. This notice may not be removed or altered from any source distribution.
19 //
20 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
21 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
22 //
23 // Thread functions
24 //
25 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
26 
27 //---------------------------------------------------------------------------
28 #ifndef ZenLib_ThreadH
29 #define ZenLib_ThreadH
30 //---------------------------------------------------------------------------
31 #include "ZenLib/Conf.h"
32 #include "ZenLib/CriticalSection.h"
33 #ifdef _WINDOWS
34  #undef Yield
35 #endif
36 //---------------------------------------------------------------------------
37 
38 namespace ZenLib
39 {
40 
41 //***************************************************************************
42 /// @brief Thread manipulation
43 //***************************************************************************
44 
45 class Thread
46 {
47 public :
48  //Constructor/Destructor
49  Thread ();
50  virtual ~Thread ();
51 
52  //Control
54  {
55  Ok,
59  };
60  returnvalue Run();
65 
66  //Status
67  bool IsRunning();
68  bool IsTerminating();
69  bool IsExited();
70 
71  //Configuration
72  void Priority_Set(int8s Priority); //-100 to +100
73 
74  //Main Entry
75  virtual void Entry();
76 
77  //Internal
78  returnvalue Internal_Exit(); //Do not use it
79 
80 protected :
81 
82  //Communicating
83  void Sleep(size_t Millisecond);
84  void Yield();
85 
86 private :
87  //Internal
88  void* ThreadPointer;
89 
90  //The possible states of the thread ("-->" shows all possible transitions from this state)
91  enum state
92  {
93  State_New, // didn't start execution yet (--> Running)
94  State_Running, // thread is running (--> Paused, Terminating)
95  State_Paused, // thread is temporarily suspended (--> Running)
96  State_Terminating, // thread should terminate a.s.a.p. (--> Terminated)
97  State_Terminated, // thread is terminated
98  };
99  state State;
100  CriticalSection C;
101 };
102 
103 } //NameSpace
104 
105 #endif