fastdo  0.6.8
threads.hpp
浏览该文件的文档.
1 #ifndef __THREADS_HPP__
2 #define __THREADS_HPP__
3 //
4 // threads 提供线程相关的功能
5 //
6 
7 namespace winux
8 {
9 
10 //=========================================================================================
14 class ThreadSysError : public Error
15 {
16 public:
17  ThreadSysError( int errCode, AnsiString const & errStr ) throw() : Error( errCode, errStr ) { }
19  int getErrCode() const throw() { return this->getErrType(); }
20 };
21 
22 //=========================================================================================
23 // 线程相关错误返回值为errno.h定义的错误码
24 //=========================================================================================
25 
28 {
29 public:
31  explicit ThreadAttr( bool isCreate = true );
32  ~ThreadAttr();
33 
34  ThreadAttr( ThreadAttr && other );
35  ThreadAttr & operator = ( ThreadAttr && other );
36 
38  int create();
39 
41  int destroy();
42 
44  operator bool() const;
45 
46 public:
47  //attributes:
48 
51  {
52  threadCreateJoinable = 0,
53  threadCreateDetached = 1,
54  };
56  void setDetachState( DetachStateType detachState = threadCreateJoinable );
58  DetachStateType getDetachState() const;
59 
60  void setStackSize( size_t stackSize );
61  size_t getStackSize() const;
62 
63 private:
65  friend class Thread;
66 
68 };
69 
72 {
73 public:
74  ThreadId();
75  ~ThreadId();
76  ThreadId( ThreadId const & other );
77  ThreadId & operator = ( ThreadId const & other );
78  ThreadId( ThreadId && other );
79  ThreadId & operator = ( ThreadId && other );
80 
81  bool operator == ( ThreadId const & other ) const;
82  bool operator != ( ThreadId const & other ) const { return !this->operator == (other); }
83 
84 private:
86 
87  friend class Thread;
88 };
89 
90 class ThreadGroup;
93 {
94 public:
95 
97  typedef void * (* ThreadFuncPtr)( void * param );
98 
99 public:
100 
102  static ThreadId Self();
103 
105  static void Exit( void * exitVal = NULL );
106 
110  static void * Join( Thread & otherThread );
111 
112 private:
113  // 默认线程处理函数, param被传入Thread对象, 调用thObj->run()
114  static void * _ThreadDefaultFunc( void * param );
115 
116 public:
121  explicit Thread( bool isStartup = false ) : _attr(false), _exitVal(NULL), _deleter(NULL), _group(NULL), _isRunning(false)
122  {
123  if ( isStartup ) this->startup();
124  }
125 
129  template < typename _Fx, typename... _ArgType >
130  explicit Thread( bool isStartup, _Fx routine, _ArgType&&... arg ) : _attr(false), _exitVal(NULL), _deleter(NULL), _group(NULL), _isRunning(false)
131  {
132  this->setRunable( routine, std::forward<_ArgType>(arg)... );
133  if ( isStartup ) this->startup();
134  }
135 
137  virtual ~Thread() { }
138 
140  Thread( Thread && other );
141 
143  Thread & operator = ( Thread && other );
144 
149  int startup();
150 
152  template < typename _Fx, typename... _ArgType >
153  int startup( _Fx routine, _ArgType&&... arg )
154  {
155  this->setRunable( routine, std::forward<_ArgType>(arg)... );
156  return this->startup();
157  }
158 
160  int startupEx( ThreadFuncPtr startRoutine, void * param );
161 
163  void * joined();
164 
171  int detach();
172 
173 public:
174  //attributes:
175 
177  ThreadAttr & attr();
178 
180  template < typename _Fx, typename... _ArgType >
181  Thread & setRunable( _Fx routine, _ArgType&&... arg )
182  {
183  this->_runable.attachNew( NewRunable( routine, std::forward<_ArgType>(arg)... ) );
184  return *this;
185  }
186 
188  Thread & setDeleter( SimpleDeleterContext * deleter = NULL )
189  {
190  _deleter = deleter;
191  return *this;
192  }
194  Thread & setDefaultDeleter() { return this->setDeleter( new SimpleDefaultDeleterContext<Thread*>(this) ); }
196  template < typename _Dt >
197  Thread & setCustomDeleter( _Dt fn ) { return this->setDeleter( new SimpleCustomDeleterContext< Thread*, _Dt >( this, fn ) ); }
198 
200  void * getExitVal() const { return _exitVal; }
202  Thread & setExitVal( void * exitVal )
203  {
204  _exitVal = exitVal;
205  return *this;
206  }
207 
209  ThreadId get() const
210  {
211  return _threadId;
212  }
213 
214 protected:
216  virtual void run();
217 
218 private:
219  ThreadAttr _attr;
220  void * _exitVal;
221  SimpleDeleterContext * _deleter;
222  ThreadGroup * _group;
223  bool _isRunning;
224  ThreadId _threadId;
225  SimplePointer<Runable> _runable;
226 
227  friend class ThreadGroup;
229 };
230 
231 //=========================================================================================
234 {
235 public:
236  explicit MutexAttr( bool isCreate = true );
237  ~MutexAttr();
238 
239  MutexAttr( MutexAttr && other );
240  MutexAttr & operator = ( MutexAttr && other );
241 
243  int create();
244 
246  int destroy();
247 
249  operator bool() const;
250 
251 public:
254  {
258  mtxDefault
259  };
260  int setType( MutexType type );
261 
262  MutexType getType() const;
263 
264 private:
266  friend class Mutex;
267 
269 };
270 
273 {
274 public:
275  explicit Mutex( bool isCreate = false );
276  ~Mutex();
277 
278  Mutex( Mutex && other );
279  Mutex & operator = ( Mutex && other );
280 
282  virtual int create();
283 
285  int destroy();
286 
287  bool lock();
288  bool tryLock();
289  bool unlock();
290 
292  MutexAttr & attr();
293 private:
294  MutexAttr _attr;
296  friend class Condition;
297 
299 };
300 
303 {
304 public:
305  explicit RecursiveMutex( bool isCreate = false );
306  int create() override;
307 };
308 
309 //=========================================================================================
312 {
313 public:
314  explicit ConditionAttr( bool isCreate = true );
315  ~ConditionAttr();
316 
317  ConditionAttr( ConditionAttr && other );
318  ConditionAttr & operator = ( ConditionAttr && other );
319 
321  int create();
322 
324  int destroy();
325 
327  operator bool() const;
328 
329 private:
331  friend class Condition;
332 
334 };
335 
338 {
339 public:
340  explicit Condition( bool isCreate = false );
341  ~Condition();
342 
343  Condition( Condition && other );
344  Condition & operator = ( Condition && other );
345 
347  int create();
348 
350  int destroy();
351 
356  bool wait( Mutex & mutex, double sec = -1 );
357 
362  template < typename _Predicate >
363  bool waitUntil( _Predicate pred, Mutex & mutex, double sec = -1 )
364  {
365  while ( !pred() )
366  if ( !this->wait( mutex, sec ) )
367  return pred();
368  return true;
369  }
370 
372  int notify();
373 
375  int notifyAll();
376 
377  ConditionAttr & attr();
378 
379 private:
380  ConditionAttr _attr;
383 };
384 
385 //===========================================================================================
388 {
389 public:
390  explicit TlsKey( void (*destructor)( void *pv ) = NULL );
391  ~TlsKey();
392 
393  TlsKey( TlsKey && other );
394  TlsKey & operator = ( TlsKey && other );
395 
397  int create( void (*destructor)( void *pv ) = NULL );
398 
400  int destroy();
401 
403  void * get() const;
404 
405 private:
407 
408  friend class TlsVar;
410 };
411 
414 {
415 public:
416  explicit TlsVar( TlsKey & tlsKey );
417  ~TlsVar();
418 
419  TlsVar( TlsVar && other );
420  TlsVar & operator = ( TlsVar && other );
421 
422  void * get();
423  void * get() const;
424  void set( void * v );
425 
426  template < typename _Ty >
427  _Ty get() { return reinterpret_cast<_Ty>( this->get() ); }
428  template < typename _Ty >
429  _Ty get() const { return reinterpret_cast<_Ty>( this->get() ); }
430 
431  template < typename _Ty >
432  void set( _Ty v ) { this->set( reinterpret_cast<void*>(v) ); }
433 
434  template < typename _Ty >
435  _Ty & ref() { return *reinterpret_cast<_Ty*>( this->get() ); }
436  template < typename _Ty >
437  _Ty const & ref() const { return *reinterpret_cast<_Ty*>( this->get() ); }
438 
439 private:
440  TlsKey * _tlsKey;
442 };
443 
444 //===========================================================================================
445 
450 {
451 public:
452 
454  ThreadGroup() : _mtxGroup(true), _cdtGroup(true)
455  {
456  }
457 
459  template < typename _Fx, typename... _ArgType >
460  ThreadGroup( int count, _Fx fn, _ArgType&&... arg ) : _mtxGroup(true), _cdtGroup(true)
461  {
462  int i;
463  for ( i = 0; i < count; i++ )
464  {
465  Thread * p = new Thread( false, fn, std::forward<_ArgType>(arg)... );
466  p->_group = this;
467  _threads.emplace_back(p);
468  }
469  }
470 
471  virtual ~ThreadGroup()
472  {
473  }
474 
475  ThreadGroup( ThreadGroup && other ) : _mtxGroup( std::move(other._mtxGroup) ), _cdtGroup( std::move(other._cdtGroup) ), _threads( std::move(other._threads) )
476  {
477  }
478 
479  ThreadGroup & operator = ( ThreadGroup && other )
480  {
481  if ( this != &other )
482  {
483  // 先释放自身
484  this->wait();
485 
486  _mtxGroup = std::move(other._mtxGroup);
487  _cdtGroup = std::move(other._cdtGroup);
488  _threads = std::move(other._threads);
489  }
490  return *this;
491  }
492 
494  ThreadGroup & destroy();
495 
497  template < typename _Fx, typename... _ArgType >
498  ThreadGroup & create( int count, _Fx fn, _ArgType&&... arg )
499  {
500  this->destroy();
501 
502  int i;
503  for ( i = 0; i < count; i++ )
504  {
505  Thread * p = new Thread( false, fn, std::forward<_ArgType>(arg)... );
506  p->_group = this;
507  _threads.emplace_back(p);
508  }
509  return *this;
510  }
511 
513  template < class _ThreadCls >
514  ThreadGroup & create( int count )
515  {
516  this->destroy();
517 
518  int i;
519  for ( i = 0; i < count; i++ )
520  {
521  Thread * p = new _ThreadCls();
522  p->_group = this;
523  _threads.emplace_back(p);
524  }
525  return *this;
526  }
527 
529  ThreadGroup & startup();
530 
534  bool wait( double sec = -1 );
535 
536 private:
537  static void * _ThreadGroupDefaultFunc( void * param );
538 
539  Mutex _mtxGroup; // 互斥量保护数据
540  Condition _cdtGroup; // 用于判断组中线程是否全部运行完毕
541  std::vector< SimplePointer<Thread> > _threads;
542 
544 };
545 
546 } // namespace winux
547 
548 #endif // __THREADS_HPP__
XString< char > AnsiString
Definition: utilities.hpp:212
Thread(bool isStartup, _Fx routine, _ArgType &&...arg)
构造函数2
Definition: threads.hpp:130
RunableT< _Fx, std::tuple< typename std::decay< _ArgType >::type... > > * NewRunable(_Fx fn, _ArgType &&...arg)
创建一个Runable对象
Definition: utilities.hpp:119
ThreadGroup(ThreadGroup &&other)
Definition: threads.hpp:475
同步锁对象接口
Definition: system.hpp:197
ThreadGroup & create(int count, _Fx fn, _ArgType &&...arg)
按指定的线程处理例程,创建一定数量的线程
Definition: threads.hpp:498
ThreadGroup(int count, _Fx fn, _ArgType &&...arg)
构造函数2 提供一个线程处理例程,并指定创建的线程数量
Definition: threads.hpp:460
Simple默认删除器场景
Definition: smartptr.hpp:48
#define WINUX_DLL
Definition: utilities.hpp:60
void * getExitVal() const
取得退出值
Definition: threads.hpp:200
Simple自定义删除器场景
Definition: smartptr.hpp:61
MutexType
互斥锁类型
Definition: threads.hpp:253
Thread & setCustomDeleter(_Dt fn)
设置自定义删除器场景以便默认线程函数删除Thread对象自己
Definition: threads.hpp:197
Thread & setDefaultDeleter()
设置默认删除器场景以便默认线程函数删除Thread对象自己
Definition: threads.hpp:194
STL namespace.
ThreadGroup & create(int count)
创建一定数量指定的派生类线程
Definition: threads.hpp:514
Thread(bool isStartup=false)
构造函数1
Definition: threads.hpp:121
条件变量
Definition: threads.hpp:337
_Ty & ref()
Definition: threads.hpp:435
Simple删除器场景基类
Definition: smartptr.hpp:26
#define DISABLE_OBJECT_COPY(clsname)
Definition: utilities.hpp:81
virtual ~ThreadGroup()
Definition: threads.hpp:471
互斥量
Definition: threads.hpp:272
互斥量属性
Definition: threads.hpp:233
条件变量属性
Definition: threads.hpp:311
线程组
Definition: threads.hpp:449
普通互斥锁
Definition: threads.hpp:255
bool waitUntil(_Predicate pred, Mutex &mutex, double sec=-1)
等待谓词条件达成
Definition: threads.hpp:363
ThreadSysError(int errCode, AnsiString const &errStr)
Definition: threads.hpp:17
ThreadGroup()
构造函数1 默认
Definition: threads.hpp:454
TLS Var.
Definition: threads.hpp:413
线程
Definition: threads.hpp:92
Thread & setRunable(_Fx routine, _ArgType &&...arg)
设置Runable,run()默认会调用它
Definition: threads.hpp:181
线程相关错误
Definition: threads.hpp:14
线程ID
Definition: threads.hpp:71
int getErrCode() const
获取错误代码
Definition: threads.hpp:19
Thread & setExitVal(void *exitVal)
设置退出值,可在run()中调用
Definition: threads.hpp:202
virtual int getErrType() const
Definition: utilities.hpp:514
简单指针
Definition: smartptr.hpp:235
错误类
Definition: utilities.hpp:505
TLS Key.
Definition: threads.hpp:387
Thread & setDeleter(SimpleDeleterContext *deleter=NULL)
设置删除器场景以便默认线程函数删除Thread对象自己
Definition: threads.hpp:188
DetachStateType
分离状态类型
Definition: threads.hpp:50
virtual ~Thread()
析构函数
Definition: threads.hpp:137
_Ty const & ref() const
Definition: threads.hpp:437
线程属性
Definition: threads.hpp:27
跨平台基础功能库
Definition: archives.hpp:7
int startup(_Fx routine, _ArgType &&...arg)
实际创建一个线程,提供你自己的处理例程
Definition: threads.hpp:153