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

114 lines
2.7 KiB
C++
Raw Normal View History

#pragma once
#include "../acl_cpp_define.hpp"
#include "noncopyable.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>
2014-11-19 00:25:21 +08:00
*/
class ACL_CPP_API thread_job : public noncopyable
2014-11-19 00:25:21 +08:00
{
public:
thread_job(void) {}
virtual ~thread_job(void) {}
2014-11-19 00:25:21 +08:00
/**
* <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>
2014-11-19 00:25:21 +08:00
*/
virtual void* run(void) = 0;
2020-09-16 16:26:00 +08:00
/**
* <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>ͬ<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> start() <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǰ<EFBFBD><EFBFBD><EFBFBD>߳<EFBFBD>ִ<EFBFBD>г<EFBFBD>ʼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̡<EFBFBD>
*/
virtual void init(void) {}
2014-11-19 00:25:21 +08:00
};
template<typename T> class tbox;
class atomic_long;
2014-11-19 00:25:21 +08:00
/**
* <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>
2014-11-19 00:25:21 +08:00
*/
class ACL_CPP_API thread : public thread_job
{
public:
thread(void);
virtual ~thread(void);
2014-11-19 00:25:21 +08:00
/**
* <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>
2014-11-19 00:25:21 +08:00
*/
bool start(bool sync = false);
2014-11-19 00:25:21 +08:00
/**
* <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>
2014-11-19 00:25:21 +08:00
*/
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>״̬<EFBFBD><EFBFBD><EFBFBD>ڷǷ<EFBFBD><EFBFBD><EFBFBD>״
* ̬<EFBFBD>£<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>߳̿<EFBFBD><EFBFBD><EFBFBD> wait <EFBFBD><EFBFBD><EFBFBD>̶߳<EFBFBD><EFBFBD>󣬷<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֹ wait <EFBFBD><EFBFBD><EFBFBD>̶߳<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڷ<EFBFBD>
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD>״̬<EFBFBD>£<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̱߳<EFBFBD><EFBFBD><EFBFBD>Ҫ wait <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>״̬
2014-11-19 00:25:21 +08:00
* @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>ֵ
2014-11-19 00:25:21 +08:00
* @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>
2014-11-19 00:25:21 +08:00
* @return {unsigned long}
*/
unsigned long thread_id(void) const;
2014-11-19 00:25:21 +08:00
/**
* <EFBFBD><EFBFBD>ǰ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̵߳<EFBFBD><EFBFBD>߳<EFBFBD> id <EFBFBD><EFBFBD>
2014-11-19 00:25:21 +08:00
* @return {unsigned long}
*/
static unsigned long thread_self(void);
static unsigned long self(void)
2017-10-06 23:19:37 +08:00
{
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
tbox<int>* sync_;
atomic_long* lock_;
2014-11-19 00:25:21 +08:00
void* return_arg_;
static void* thread_run(void* arg);
void wait_for_running(void);
2014-11-19 00:25:21 +08:00
};
} // namespace acl