UniSet  2.24.2
OmniThreadCreator.h
1 /*
2  * Copyright (c) 2015 Pavel Vainerman.
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as
6  * published by the Free Software Foundation, version 2.1.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  * Lesser General Lesser Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  */
16 // --------------------------------------------------------------------------
21 // --------------------------------------------------------------------------
22 #ifndef OmniThreadCreator_h_
23 #define OmniThreadCreator_h_
24 //---------------------------------------------------------------------------
25 #include <omnithread.h>
26 #include <memory>
27 //----------------------------------------------------------------------------
28 namespace uniset
29 {
89  //----------------------------------------------------------------------------------------
90  template<class ThreadMaster>
92  public omni_thread
93  {
94  public:
95 
97  typedef void(ThreadMaster::* Action)();
98 
99  OmniThreadCreator( const std::shared_ptr<ThreadMaster>& m, Action a, bool undetached = false );
100  virtual ~OmniThreadCreator() {}
101 
102  inline bool isRunning()
103  {
104  return omni_thread::state() == omni_thread::STATE_RUNNING;
105  }
106  inline void stop()
107  {
108  omni_thread::exit(0);
109  }
110  inline int getTID()
111  {
112  return omni_thread::id();
113  }
114 
115  inline void join()
116  {
117  omni_thread::join(NULL);
118  }
119 
120  protected:
121  void* run_undetached(void* x)
122  {
123  if(m)
124  (m.get()->*act)();
125 
126  return (void*)0;
127  }
128 
129  virtual void run(void* arg)
130  {
131  if(m)
132  (m.get()->*act)();
133  }
134 
135  private:
136  OmniThreadCreator();
137  std::shared_ptr<ThreadMaster> m;
138  Action act;
139  };
140 
141  //----------------------------------------------------------------------------------------
142  template <class ThreadMaster>
143  OmniThreadCreator<ThreadMaster>::OmniThreadCreator( const std::shared_ptr<ThreadMaster>& _m, Action a, bool undetach ):
144  omni_thread(),
145  m(_m),
146  act(a)
147  {
148  if(undetach)
149  start_undetached();
150  }
151  //----------------------------------------------------------------------------------------
152 
153  template <class ThreadMaster>
154  OmniThreadCreator<ThreadMaster>::OmniThreadCreator():
155  m(0),
156  act(0)
157  {
158  }
159  //----------------------------------------------------------------------------------------
160 } // end of uniset namespace
161 // -------------------------------------------------------------------------
162 #endif // OmniThreadCreator_h_
Definition: OmniThreadCreator.h:93
void(ThreadMaster::* Action)()
Definition: OmniThreadCreator.h:97
Definition: CommonEventLoop.h:15