Android C++ 线程使用
来源:互联网 发布:无线网络规划软件 编辑:程序博客网 时间:2024/06/09 15:34
Android对Linux线程提供了C++封装Thread类,它是线程的基类。使用Thread类,需创建一个新类继承于Thread类,并实现threadLoop()方法,它即是线程函数。要启动线程,调用run()函数即可。 /*
* Android线程封装的基类
*/
class Thread : virtual public RefBase
{
public:
Thread(bool canCallJava = true);
virtual ~Thread();
/* 启动线程,即创建一个新的线程并执行threadLoop()虚函数 */
virtual status_t run( const char* name = 0,
int32_t priority = PRIORITY_DEFAULT,
size_t stack = 0);
/* 要求退出线程(这个函数是异步的) */
virtual void requestExit();
/* 可以重载此虚函数以进行初始化工作,但必须显示调用 */
virtual status_t readyToRun();
/* 要求线程退出(同步的) */
status_t requestExitAndWait();
protected:
/* 判断requestExit()是否被调用过 */
bool exitPending() const;
private:
/* 线程函数。若此函数返回true,当requestExit()没被调用过时会在次调用此函数;若返回false,
* 在该函数返回时线程将退出
*/
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
};
* Android线程封装的基类
*/
class Thread : virtual public RefBase
{
public:
Thread(bool canCallJava = true);
virtual ~Thread();
/* 启动线程,即创建一个新的线程并执行threadLoop()虚函数 */
virtual status_t run( const char* name = 0,
int32_t priority = PRIORITY_DEFAULT,
size_t stack = 0);
/* 要求退出线程(这个函数是异步的) */
virtual void requestExit();
/* 可以重载此虚函数以进行初始化工作,但必须显示调用 */
virtual status_t readyToRun();
/* 要求线程退出(同步的) */
status_t requestExitAndWait();
protected:
/* 判断requestExit()是否被调用过 */
bool exitPending() const;
private:
/* 线程函数。若此函数返回true,当requestExit()没被调用过时会在次调用此函数;若返回false,
* 在该函数返回时线程将退出
*/
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
};
- android下线程使用
- Android 使用后台线程
- Android C++ 线程使用
- android线程使用
- Android线程池使用
- Android线程池使用
- Android 线程池使用
- Android线程池使用
- Android线程的使用
- Android中使用线程
- android 线程池使用
- Android线程池使用
- 如何使用线程池(C#)
- Linux C线程的使用
- Android中轻松使用线程
- 使用Android AsyncTask异步线程
- android 异步线程的使用
- Android中线程的使用
- C++随机数
- json格式解析在android的应用
- hdu 2111 多重背包
- 周迅与李大齐分手后首谈旧爱:诚实面对自己的想法
- tomcat内存溢出原因分析与解决
- Android C++ 线程使用
- 模板编译原型
- JOS学习笔记(三)
- 模板实例化
- HDU 4436 str2int
- TCP与UDP疑难杂症
- wp8开发者账号获取以及wp手机解锁
- 大学ACM的总结
- UNIX网络编程中RST分节