1 #ifndef __THREADS_HPP__ 2 #define __THREADS_HPP__ 44 operator bool()
const;
52 threadCreateJoinable = 0,
53 threadCreateDetached = 1,
56 void setDetachState(
DetachStateType detachState = threadCreateJoinable );
60 void setStackSize(
size_t stackSize );
61 size_t getStackSize()
const;
81 bool operator == (
ThreadId const & other )
const;
82 bool operator != (
ThreadId const & other )
const {
return !this->operator == (other); }
97 typedef void * (* ThreadFuncPtr)(
void * param );
105 static void Exit(
void * exitVal = NULL );
110 static void * Join(
Thread & otherThread );
114 static void * _ThreadDefaultFunc(
void * param );
121 explicit Thread(
bool isStartup =
false ) : _attr(false), _exitVal(NULL), _deleter(NULL), _group(NULL), _isRunning(false)
123 if ( isStartup ) this->startup();
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)
132 this->setRunable( routine, std::forward<_ArgType>(arg)... );
133 if ( isStartup ) this->startup();
152 template <
typename _Fx,
typename... _ArgType >
155 this->setRunable( routine, std::forward<_ArgType>(arg)... );
156 return this->startup();
160 int startupEx( ThreadFuncPtr startRoutine,
void * param );
180 template <
typename _Fx,
typename... _ArgType >
183 this->_runable.attachNew(
NewRunable( routine, std::forward<_ArgType>(arg)... ) );
196 template <
typename _Dt >
236 explicit MutexAttr(
bool isCreate =
true );
249 operator bool()
const;
275 explicit Mutex(
bool isCreate =
false );
282 virtual int create();
306 int create()
override;
327 operator bool()
const;
340 explicit Condition(
bool isCreate =
false );
356 bool wait(
Mutex & mutex,
double sec = -1 );
362 template <
typename _Predicate >
366 if ( !this->wait( mutex, sec ) )
390 explicit TlsKey(
void (*destructor)(
void *pv ) = NULL );
397 int create(
void (*destructor)(
void *pv ) = NULL );
424 void set(
void * v );
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() ); }
431 template <
typename _Ty >
432 void set( _Ty v ) { this->
set(
reinterpret_cast<void*
>(v) ); }
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() ); }
459 template <
typename _Fx,
typename... _ArgType >
460 ThreadGroup(
int count, _Fx fn, _ArgType&&... arg ) : _mtxGroup(true), _cdtGroup(true)
463 for ( i = 0; i < count; i++ )
465 Thread * p =
new Thread(
false, fn, std::forward<_ArgType>(arg)... );
467 _threads.emplace_back(p);
481 if (
this != &other )
486 _mtxGroup = std::move(other._mtxGroup);
487 _cdtGroup = std::move(other._cdtGroup);
488 _threads = std::move(other._threads);
497 template <
typename _Fx,
typename... _ArgType >
503 for ( i = 0; i < count; i++ )
505 Thread * p =
new Thread(
false, fn, std::forward<_ArgType>(arg)... );
507 _threads.emplace_back(p);
513 template <
class _ThreadCls >
519 for ( i = 0; i < count; i++ )
521 Thread * p =
new _ThreadCls();
523 _threads.emplace_back(p);
534 bool wait(
double sec = -1 );
537 static void * _ThreadGroupDefaultFunc(
void * param );
541 std::vector< SimplePointer<Thread> > _threads;
548 #endif // __THREADS_HPP__ XString< char > AnsiString
Thread(bool isStartup, _Fx routine, _ArgType &&...arg)
构造函数2
RunableT< _Fx, std::tuple< typename std::decay< _ArgType >::type... > > * NewRunable(_Fx fn, _ArgType &&...arg)
创建一个Runable对象
ThreadGroup(ThreadGroup &&other)
ThreadGroup & create(int count, _Fx fn, _ArgType &&...arg)
按指定的线程处理例程,创建一定数量的线程
ThreadGroup(int count, _Fx fn, _ArgType &&...arg)
构造函数2 提供一个线程处理例程,并指定创建的线程数量
void * getExitVal() const
取得退出值
Thread & setCustomDeleter(_Dt fn)
设置自定义删除器场景以便默认线程函数删除Thread对象自己
Thread & setDefaultDeleter()
设置默认删除器场景以便默认线程函数删除Thread对象自己
ThreadGroup & create(int count)
创建一定数量指定的派生类线程
Thread(bool isStartup=false)
构造函数1
#define DISABLE_OBJECT_COPY(clsname)
bool waitUntil(_Predicate pred, Mutex &mutex, double sec=-1)
等待谓词条件达成
ThreadSysError(int errCode, AnsiString const &errStr)
Thread & setRunable(_Fx routine, _ArgType &&...arg)
设置Runable,run()默认会调用它
int getErrCode() const
获取错误代码
Thread & setExitVal(void *exitVal)
设置退出值,可在run()中调用
virtual int getErrType() const
Thread & setDeleter(SimpleDeleterContext *deleter=NULL)
设置删除器场景以便默认线程函数删除Thread对象自己
int startup(_Fx routine, _ArgType &&...arg)
实际创建一个线程,提供你自己的处理例程