1 #ifndef __THREADS_HPP__ 2 #define __THREADS_HPP__ 54 static int Yield(
void);
81 operator bool()
const;
89 threadCreateJoinable = 0,
90 threadCreateDetached = 1,
93 void setDetachState(
DetachStateType detachState = threadCreateJoinable );
98 void setStackSize(
size_t stackSize );
100 size_t getStackSize()
const;
138 bool operator == (
ThreadId const & other )
const;
139 bool operator != (
ThreadId const & other )
const {
return !this->operator == (other); }
153 typedef void * (* ThreadFuncPtr)(
void * param );
160 static void Exit(
void * exitVal = NULL );
165 static void * Join(
Thread & otherThread );
171 static void SetConcurrency(
int newLevel );
173 static int GetConcurrency(
void );
177 static void * _ThreadDefaultFunc(
void * param );
185 explicit Thread(
bool isStartup =
false ) : _param(nullptr), _attr(false), _exitVal(nullptr), _deleter(nullptr), _group(nullptr), _isRunning(false)
187 if ( isStartup ) this->startup();
193 template <
typename _Fx,
typename... _ArgType >
194 explicit Thread(
bool isStartup, _Fx routine, _ArgType&&... arg ) : _param(nullptr), _attr(false), _exitVal(nullptr), _deleter(nullptr), _group(nullptr), _isRunning(false)
196 this->setRunable( routine, std::forward<_ArgType>(arg)... );
197 if ( isStartup ) this->startup();
216 template <
typename _Fx,
typename... _ArgType >
219 this->setRunable( routine, std::forward<_ArgType>(arg)... );
220 return this->startup();
224 int startupEx( ThreadFuncPtr startRoutine,
void * param );
247 void getSchedParam(
SchedulePolicy * policy,
int * priority )
const;
253 template <
typename _Fx,
typename... _ArgType >
256 this->_runable.attachNew(
NewRunable( routine, std::forward<_ArgType>(arg)... ) );
271 template <
typename _Dt >
313 explicit MutexAttr(
bool isCreate =
true );
326 operator bool()
const;
352 explicit Mutex(
bool isCreate =
false );
359 virtual int create();
383 int create()
override;
404 operator bool()
const;
417 explicit Condition(
bool isCreate =
false );
433 bool wait(
Mutex & mutex,
double sec = -1 );
439 template <
typename _Predicate >
443 if ( !this->wait( mutex, sec ) )
467 explicit TlsKey(
void (*destructor)(
void *pv ) = NULL );
474 int create(
void (*destructor)(
void *pv ) = NULL );
501 void set(
void * v );
503 template <
typename _Ty >
504 _Ty
get() {
return reinterpret_cast<_Ty
>( this->
get() ); }
505 template <
typename _Ty >
506 _Ty
get()
const {
return reinterpret_cast<_Ty
>( this->
get() ); }
508 template <
typename _Ty >
509 void set( _Ty v ) { this->
set(
reinterpret_cast<void*
>(v) ); }
511 template <
typename _Ty >
512 _Ty &
ref() {
return *
reinterpret_cast<_Ty*
>( this->
get() ); }
513 template <
typename _Ty >
514 _Ty
const &
ref()
const {
return *
reinterpret_cast<_Ty*
>( this->
get() ); }
536 template <
typename _Fx,
typename... _ArgType >
537 ThreadGroup(
size_t count, _Fx fn, _ArgType&&... arg ) : _mtxGroup(true), _cdtGroup(true)
539 for (
size_t i = 0; i < count; i++ )
541 Thread * p =
new Thread(
false, fn, std::forward<_ArgType>(arg)... );
543 _threads.emplace_back(p);
557 if (
this != &other )
562 _mtxGroup = std::move(other._mtxGroup);
563 _cdtGroup = std::move(other._cdtGroup);
564 _threads = std::move(other._threads);
573 template <
typename _Fx,
typename... _ArgType >
578 for (
size_t i = 0; i < count; i++ )
580 Thread * p =
new Thread(
false, fn, std::forward<_ArgType>(arg)... );
582 _threads.emplace_back(p);
588 template <
class _ThreadCls >
593 for (
size_t i = 0; i < count; i++ )
595 Thread * p =
new _ThreadCls();
597 _threads.emplace_back(p);
608 bool wait(
double sec = -1 );
611 static void * _ThreadGroupDefaultFunc(
void * param );
615 std::vector< SimplePointer<Thread> > _threads;
623 #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)
它是一种实时的先进先出调用策略,且只能在超级用户下运行。这种调用策略仅仅被使用于优先级大于0的线程。它...
void * getExitVal() const
取得退出值
Thread & setCustomDeleter(_Dt fn)
设置自定义删除器场景以便默认线程函数删除Thread对象自己
Thread & setDefaultDeleter()
设置默认删除器场景以便默认线程函数删除Thread对象自己
Thread(bool isStartup=false)
构造函数1
ThreadGroup(size_t count, _Fx fn, _ArgType &&...arg)
构造函数2 提供一个线程处理例程,并指定创建的线程数量
#define DISABLE_OBJECT_COPY(clsname)
它是默认的线程分时调度策略,所有的线程的优先级别都是0,线程的调度是通过分时来完成的。如果系统使用这种...
bool waitUntil(_Predicate pred, Mutex &mutex, double sec=-1)
等待谓词条件达成
ThreadSysError(int errCode, AnsiString const &errStr)
Thread & setRunable(_Fx routine, _ArgType &&...arg)
设置Runable,run()默认会调用它
struct _ThreadFuncParam * _param
线程函数的参数
int getErrCode() const
获取错误代码
Thread & setExitVal(void *exitVal)
设置退出值,可在run()中调用
ThreadGroup & create(size_t count)
创建一定数量指定的派生类线程
virtual int getErrType() const
Thread & setDeleter(SimpleDeleterContext *deleter=NULL)
设置删除器场景以便默认线程函数删除Thread对象自己
ThreadGroup & create(size_t count, _Fx fn, _ArgType &&...arg)
按指定的线程处理例程,创建一定数量的线程
鉴于SCHED_FIFO调度策略的一些缺点,SCHED_RR对SCHED_FIFO做出了一些增强功能。从实质上看,它还是SCHED_FIF...
int startup(_Fx routine, _ArgType &&...arg)
实际创建一个线程,提供你自己的处理例程