acl/lib_acl_cpp/include/acl_cpp/stdlib/thread.hpp

97 lines
2.1 KiB
C++
Raw Normal View History

2014-11-19 00:25:21 +08:00
#pragma once
#include "../acl_cpp_define.hpp"
2014-11-19 00:25:21 +08:00
namespace acl
{
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>߳<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD> run <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>߳<EFBFBD><EFBFBD>б<EFBFBD>ִ<EFBFBD>е<EFBFBD>
*/
class ACL_CPP_API thread_job
{
public:
thread_job() {}
virtual ~thread_job() {}
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD>ִ˺<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ú<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>߳<EFBFBD><EFBFBD><EFBFBD>ִ<EFBFBD><EFBFBD>
* @return {void*} <EFBFBD>߳<EFBFBD><EFBFBD>˳<EFBFBD>ǰ<EFBFBD><EFBFBD><EFBFBD>صIJ<EFBFBD><EFBFBD><EFBFBD>
*/
virtual void* run() = 0;
};
/**
* <EFBFBD>̴߳<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ľӿڶ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> Java <EFBFBD>Ľӿڶ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫʵ<EFBFBD><EFBFBD>
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ĵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͨ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> thread::start() <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̹߳<EFBFBD><EFBFBD><EFBFBD>
*/
class ACL_CPP_API thread : public thread_job
{
public:
thread();
virtual ~thread();
/**
* <EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̹߳<EFBFBD><EFBFBD>̣<EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD>ú<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ã<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD>µ<EFBFBD>
* <EFBFBD><EFBFBD><EFBFBD>̣߳<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>߳<EFBFBD><EFBFBD><EFBFBD>ִ<EFBFBD>л<EFBFBD><EFBFBD><EFBFBD> thread_job::run <EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @return {bool} <EFBFBD>Ƿ<EFBFBD><EFBFBD>ɹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>߳<EFBFBD>
*/
bool start();
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>߳<EFBFBD>ʱΪ<EFBFBD><EFBFBD> detachable ״̬<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ô˺<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȴ<EFBFBD><EFBFBD>߳̽<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>߳<EFBFBD>ʱΪ detachable ״̬ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֹ<EFBFBD><EFBFBD><EFBFBD>ñ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param out {void**} <EFBFBD><EFBFBD><EFBFBD>ò<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǿ<EFBFBD>ָ<EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD>ò<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* <EFBFBD>߳<EFBFBD><EFBFBD>˳<EFBFBD>ǰ<EFBFBD><EFBFBD><EFBFBD>صIJ<EFBFBD><EFBFBD><EFBFBD>
* @return {bool} <EFBFBD>Ƿ<EFBFBD><EFBFBD>ɹ<EFBFBD>
*/
bool wait(void** out = NULL);
/**
* <EFBFBD>ڵ<EFBFBD><EFBFBD><EFBFBD> start ǰ<EFBFBD><EFBFBD><EFBFBD>ô˺<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>߳<EFBFBD><EFBFBD>Ƿ<EFBFBD>Ϊ
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD> (detachable) ״̬<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>δ<EFBFBD><EFBFBD><EFBFBD>ô˺<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* <EFBFBD><EFBFBD><EFBFBD>߳<EFBFBD>Ĭ<EFBFBD><EFBFBD>Ϊ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>״̬
* @param yes {bool} <EFBFBD>Ƿ<EFBFBD>Ϊ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>״̬
* @return {thread&}
*/
thread& set_detachable(bool yes);
/**
* <EFBFBD>ڵ<EFBFBD><EFBFBD><EFBFBD> start ǰ<EFBFBD><EFBFBD><EFBFBD>ô˺<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̵߳Ķ<EFBFBD>ջ<EFBFBD><EFBFBD>С
* @param size {size_t} <EFBFBD>̶߳<EFBFBD>ջ<EFBFBD><EFBFBD>С<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵΪ 0 <EFBFBD><EFBFBD>δ
* <EFBFBD><EFBFBD><EFBFBD>ô˺<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̶߳<EFBFBD>ջ<EFBFBD><EFBFBD>СΪϵͳ<EFBFBD><EFBFBD>Ĭ<EFBFBD><EFBFBD>ֵ
* @return {thread&}
*/
thread& set_stacksize(size_t size);
/**
* <EFBFBD>ڵ<EFBFBD><EFBFBD><EFBFBD> start <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ô˺<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ի<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̵߳<EFBFBD> id <EFBFBD><EFBFBD>
* @return {unsigned long}
*/
unsigned long thread_id() const;
/**
* <EFBFBD><EFBFBD>ǰ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̵߳<EFBFBD><EFBFBD>߳<EFBFBD> id <EFBFBD><EFBFBD>
* @return {unsigned long}
*/
static unsigned long thread_self();
2017-10-06 23:19:37 +08:00
static unsigned long self()
{
return thread_self();
}
2014-11-19 00:25:21 +08:00
private:
bool detachable_;
size_t stack_size_;
#if defined(_WIN32) || defined(_WIN64)
2014-11-19 00:25:21 +08:00
void* thread_;
unsigned long thread_id_;
#else
pthread_t thread_;
unsigned long thread_id_;
#endif
void* return_arg_;
static void* thread_run(void* arg);
};
} // namespace acl