2015-06-18 14:54:58 +08:00
|
|
|
|
#pragma once
|
2017-06-02 14:47:24 +08:00
|
|
|
|
#include "../acl_cpp_define.hpp"
|
2017-11-14 10:52:44 +08:00
|
|
|
|
#include "noncopyable.hpp"
|
2015-06-18 14:54:58 +08:00
|
|
|
|
|
|
|
|
|
struct ACL_AQUEUE;
|
|
|
|
|
|
|
|
|
|
namespace acl
|
|
|
|
|
{
|
|
|
|
|
|
2019-05-09 13:57:51 +08:00
|
|
|
|
class ACL_CPP_API thread_qitem : public noncopyable
|
2015-06-18 14:54:58 +08:00
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
thread_qitem() {}
|
|
|
|
|
virtual ~thread_qitem() {}
|
|
|
|
|
};
|
|
|
|
|
|
2019-05-09 13:57:51 +08:00
|
|
|
|
class ACL_CPP_API thread_queue : public noncopyable
|
2015-06-18 14:54:58 +08:00
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
thread_queue();
|
|
|
|
|
~thread_queue();
|
|
|
|
|
|
|
|
|
|
bool push(thread_qitem* item);
|
|
|
|
|
thread_qitem* pop(int wait_ms = -1);
|
|
|
|
|
int qlen() const;
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
ACL_AQUEUE* queue_;
|
|
|
|
|
};
|
|
|
|
|
|
2017-11-14 10:52:44 +08:00
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
2018-06-28 21:04:54 +08:00
|
|
|
|
#if 0
|
|
|
|
|
|
2017-11-14 10:52:44 +08:00
|
|
|
|
// internal functions being used
|
|
|
|
|
void* tbox_create(void);
|
|
|
|
|
void tbox_free(void*, void (*free_fn)(void*));
|
|
|
|
|
bool tbox_push(void*, void*);
|
|
|
|
|
void* tbox_pop(void*, int);
|
|
|
|
|
size_t tbox_size(void*);
|
|
|
|
|
|
|
|
|
|
/**
|
2017-11-14 12:09:33 +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>
|
2017-11-14 10:52:44 +08:00
|
|
|
|
*
|
2017-11-14 12:09:33 +08:00
|
|
|
|
* ʾ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2017-11-14 10:52:44 +08:00
|
|
|
|
*
|
|
|
|
|
* class myobj
|
|
|
|
|
* {
|
|
|
|
|
* public:
|
|
|
|
|
* myobj(void) {}
|
|
|
|
|
* ~myobj(void) {}
|
|
|
|
|
*
|
|
|
|
|
* void test(void) { printf("hello world\r\n"); }
|
|
|
|
|
* };
|
|
|
|
|
*
|
|
|
|
|
* acl::tbox<myobj> tbox;
|
|
|
|
|
*
|
|
|
|
|
* void thread_producer(void)
|
|
|
|
|
* {
|
|
|
|
|
* myobj* o = new myobj;
|
|
|
|
|
* tbox.push(o);
|
|
|
|
|
* }
|
|
|
|
|
*
|
|
|
|
|
* void thread_consumer(void)
|
|
|
|
|
* {
|
|
|
|
|
* myobj* o = tbox.pop();
|
|
|
|
|
* o->test();
|
|
|
|
|
* delete o;
|
|
|
|
|
* }
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
|
class tbox : noncopyable
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
tbox(void)
|
|
|
|
|
{
|
|
|
|
|
tbox_ = tbox_create();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~tbox(void)
|
|
|
|
|
{
|
|
|
|
|
tbox_free(tbox_, tbox_free_fn);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2017-11-14 12:09:33 +08:00
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @param t {T*} <EFBFBD>ǿ<EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @return {bool} <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><EFBFBD>ɹ<EFBFBD>
|
2017-11-14 10:52:44 +08:00
|
|
|
|
*/
|
|
|
|
|
bool push(T* t)
|
|
|
|
|
{
|
|
|
|
|
return tbox_push(tbox_, t);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2017-11-14 12:09:33 +08:00
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @param wait_ms {int} >= 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><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @return {T*} <EFBFBD><EFBFBD> NULL <EFBFBD><EFBFBD>ʾ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2017-11-14 10:52:44 +08:00
|
|
|
|
*/
|
|
|
|
|
T* pop(int wait_ms = -1)
|
|
|
|
|
{
|
|
|
|
|
return (T*) tbox_pop(tbox_, wait_ms);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2017-11-14 12:09:33 +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>
|
2017-11-14 10:52:44 +08:00
|
|
|
|
* @return {size_t}
|
|
|
|
|
*/
|
|
|
|
|
size_t size(void) const
|
|
|
|
|
{
|
|
|
|
|
return tbox_size(tbox_);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
void* tbox_;
|
|
|
|
|
|
|
|
|
|
static void tbox_free_fn(void* o)
|
|
|
|
|
{
|
|
|
|
|
T* t = (T*) o;
|
|
|
|
|
delete t;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2018-06-28 21:04:54 +08:00
|
|
|
|
#endif
|
|
|
|
|
|
2015-06-18 14:54:58 +08:00
|
|
|
|
} // namespace acl
|