📄 wy_thread.h
字号:
/* Copyright is licensed under GNU LGPL. by I.J.Wang 2003 WyThread is a base class to handle a thread. Note: Add -lpthread -lrt to g++ linker options Warning: Thread detatchstate can not be changed*/#ifndef WY_THREAD_H__#define WY_THREAD_H__#define WY_THREAD_VERSION 31#include "wymutex.h"#include "wycond.h"#include "wystr.h"/* Thread Identifier class*/class Wy_ThreadID { static const ::pthread_t Default_ThreadID; friend class Wy_Thread; // to access Default_ThreadID public: Wy_ThreadID() WY__TSPC(); Wy_ThreadID(const Wy_ThreadID& src) WY__TSPC(); Wy_ThreadID(::pthread_t tid) WY__TSPC(); Wy_ThreadID(Wy_ThreadID& src, Wy::ByMove_t) WY__TSPC(); bool is_default(void) const WY__TSPC(); ::pthread_t _pthread_id(void) const WY__TSPC(); void reset(void) WY__TSPC(); void reset(const Wy_ThreadID& src) WY__TSPC(); void reset(::pthread_t tid) WY__TSPC(); const Wy_ThreadID& operator =(const Wy_ThreadID& tid) WY__TSPC(); bool operator==(Wy_ThreadID id) const WY__TSPC(); bool operator!=(Wy_ThreadID id) const WY__TSPC(); static bool _thread_equal(Wy_ThreadID t1, Wy_ThreadID t2) WY__TSPC(); private: ::pthread_t _tid;};namespace Wy { // [Syn] Get the descriptive string of this object. // // [Ret] The descriptive string of this object. // WyStr wrd(const Wy_ThreadID& tid);};#ifdef WY_DEBUG extern size_t wy_joined_cnt; // count that pthread_join succeeded extern size_t wy_created_cnt; // count that pthread_create succeeded extern size_t wy_act_max; // maximal active threads#endif// Wy_Thread is a base class that tmain(...) can be run in a created thread.//class Wy_Thread { public: static const char class_name[]; WY_THROW_REPLY; // declaraion of Reply // Thread state enumeration // enum ThreadState{ Null, // Object is construtced in Null state, no thread. Active, // Thread is created (begin() called) to run tmain() Stopped // Thread terminated }; enum CancelType { Deferred=PTHREAD_CANCEL_DEFERRED, Asynchronous=PTHREAD_CANCEL_ASYNCHRONOUS }; //---------------------------------------------------------------------- // Thread function tmain() should not use these public members // (except static ones) //--------------------------------------------------------------------- Wy_Thread(); virtual ~Wy_Thread() WY__TSPC(); bool is_default(void) const; Wy_ThreadID thread_id(void) const; ThreadState thread_state(void); WyRet exitcode(void) const; virtual WyRet reset(void); virtual WyRet begin(void); virtual WyRet cancel(void); // [Cancel Point] virtual WyRet wait_not(ThreadState); //---------------- // Static members //---------------- static Wy_ThreadID thread_self(void); static WyRet yield(void); static Wy_Thread* tbase_ptr(void); static bool set_cancelable(bool en); static CancelType set_canceltype(CancelType ct); // [Cancel Point] static void cancel_point(void); static void exit(WyRet) __attribute__ ((__noreturn__)); static size_t active_threads(void); // Note: A const siz_t is considered for this member. // static size_t max_threads(void) WY__TSPC(); // Class for disabling cancelability of the calling thread // class NoCancel : protected Wy__Base::NoCancel { public: // [Syn] Construct object. The cancelablity of the calling thread is // disabled in the constructor and restored in the destructor. // NoCancel() try : Wy__Base::NoCancel() { } catch(int e) { WY_THROW( WyRet(WyReply(e)) ); }; // [Syn] Destructor. The previous cancelablity of the calling // thread is resumed. // ~NoCancel() WY__TSPC() {}; private: NoCancel(const NoCancel&); // not to use const NoCancel& operator =(const NoCancel&); // not to use bool operator ==(const NoCancel&); // not to used }; // note: Internal use, for check purpose // static const Wy_ThreadID wy_main_thrd_id; // [Internal] Thread begin function // static void * wy_thread_begin_func(Wy_Thread *obj_ptr); protected: static const size_t Max_Threads=#ifdef PTHREAD_THREADS_MAX PTHREAD_THREADS_MAX<512? PTHREAD_THREADS_MAX:512;#else 512;#endif virtual WyRet tmain(void) { return(Ok); }; // Make sure there is no active thread, the thread is cancelled if necessary // cancellability of the calling thread is disabled // void tmain_close(void) WY__TSPC(); private: mutable WyCond _thrd_stat_cond;// condition that _thrd_stat set value ThreadState _thrd_stat; ::pthread_attr_t _thrd_attr; ::pthread_t _thrd_id; WyRet _exit_obj; bool _in_destroy; // destructor set this flag true mutable WyMutex _mtx; // mutex for all the private data members // [Not Cancelable] Set the calling thread to non-cancellable state // static void _non_cancellable(void) WY__TSPC(); void _reset_avars(void) WY__NOTHROW__ { _thrd_id=Wy_ThreadID::Default_ThreadID; _thrd_stat=Null; _exit_obj.reset(); }; // [Not Cancelable] tmain() return/throw invokes this function // void* _tmain_finish(const WyRet&) WY__TSPC(); // [Internal] Make sure no active thread, by cancel and/or join // void _cleanup_thread(WyLock&); friend class Wy__TOLTab; // uses Max_Threads // Hidden members // Wy_Thread(const Wy_Thread&); const Wy_Thread& operator =(const Wy_Thread&); bool operator ==(const Wy_Thread&); bool operator !=(const Wy_Thread&); virtual Wy_Thread* _alloc(WyRet&) const { return(0); }; //void* set_terminate(void*); // not defined yet //void* set_unexpected(void*); // not defined yet};#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -