gogoWebsite

Android thread encapsulation base class Thread

Updated to 5 hours ago
 /*
* Base class for Android thread encapsulation
 */
class Thread : virtual public RefBase
{
public:
                        Thread(bool canCallJava = true);
                        virtual             ~Thread();

/* Start a thread, that is, create a new thread and execute threadLoop() virtual function */
                         virtual status_t    run(    const char* name = 0,
                                int32_t priority = PRIORITY_DEFAULT,
                                size_t stack = 0);
    
/* Requires exit from the thread (this function is asynchronous) */
                        virtual void        requestExit();

/* This virtual function can be overloaded for initialization work, but the call must be displayed */
                        virtual status_t    readyToRun();
    
/* Require thread exit (synchronous) */
                        status_t    requestExitAndWait();

protected:
/* Determine whether requestExit() has been called */
                        bool        exitPending() const;
    
private:
/* Thread function. If this function returns true, the function will be called again when requestExit() has not been called; if false is returned,
* The thread will exit when the function returns
                        */
                        virtual bool        threadLoop() = 0;

private:
                        Thread& operator=(const Thread&);
                        static  int             _threadLoop(void* user);
                        const   bool            mCanCallJava;
                        thread_id_t     mThread;
                        Mutex           mLock;
                        Condition       mThreadExitedCondition;
                        status_t        mStatus;
                        volatile bool           mExitPending;
                         volatile bool           mRunning;
                        sp<Thread>      mHoldSelf;
#if HAVE_ANDROID_OS
                        int             mTid;
#endif
};