2018-06-28 21:04:54 +08:00
|
|
|
|
#pragma once
|
|
|
|
|
#include "../acl_cpp_define.hpp"
|
|
|
|
|
#include <list>
|
|
|
|
|
#include "thread_mutex.hpp"
|
|
|
|
|
#include "thread_cond.hpp"
|
|
|
|
|
#include "noncopyable.hpp"
|
|
|
|
|
|
|
|
|
|
namespace acl
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* <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>
|
|
|
|
|
*
|
|
|
|
|
* 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 : public noncopyable
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
tbox(void) : size_(0) , cond_(&lock_) {}
|
|
|
|
|
|
|
|
|
|
~tbox(void)
|
|
|
|
|
{
|
|
|
|
|
for (typename std::list<T*>::iterator it = tbox_.begin();
|
|
|
|
|
it != tbox_.end(); ++it)
|
|
|
|
|
{
|
|
|
|
|
delete *it;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @param t {T*} <EFBFBD>ǿ<EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
*/
|
|
|
|
|
void push(T* t)
|
|
|
|
|
{
|
|
|
|
|
lock_.lock();
|
|
|
|
|
tbox_.push_back(t);
|
|
|
|
|
size_++;
|
|
|
|
|
lock_.unlock();
|
|
|
|
|
cond_.notify();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* <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>
|
|
|
|
|
* @return {T*} <EFBFBD><EFBFBD> NULL <EFBFBD><EFBFBD>ʾ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
*/
|
|
|
|
|
T* pop(int wait_ms = -1)
|
|
|
|
|
{
|
|
|
|
|
long long n = ((long long) wait_ms) * 1000;
|
|
|
|
|
lock_.lock();
|
|
|
|
|
while (true)
|
|
|
|
|
{
|
|
|
|
|
T* t = peek();
|
|
|
|
|
if (t)
|
|
|
|
|
{
|
|
|
|
|
lock_.unlock();
|
|
|
|
|
return t;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!cond_.wait(n, true) && wait_ms >= 0)
|
|
|
|
|
{
|
|
|
|
|
lock_.unlock();
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* <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 {size_t}
|
|
|
|
|
*/
|
|
|
|
|
size_t size(void) const
|
|
|
|
|
{
|
2018-06-28 21:40:24 +08:00
|
|
|
|
return size_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
void lock(void)
|
|
|
|
|
{
|
2018-06-28 21:04:54 +08:00
|
|
|
|
lock_.lock();
|
2018-06-28 21:40:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void unlock(void)
|
|
|
|
|
{
|
2018-06-28 21:04:54 +08:00
|
|
|
|
lock_.unlock();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
std::list<T*> tbox_;
|
|
|
|
|
size_t size_;
|
|
|
|
|
thread_mutex lock_;
|
|
|
|
|
thread_cond cond_;
|
|
|
|
|
|
|
|
|
|
T* peek(void)
|
|
|
|
|
{
|
|
|
|
|
typename std::list<T*>::iterator it = tbox_.begin();
|
|
|
|
|
if (it == tbox_.end())
|
|
|
|
|
return NULL;
|
|
|
|
|
size_--;
|
|
|
|
|
T* t = *it;
|
|
|
|
|
tbox_.erase(it);
|
|
|
|
|
return t;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace acl
|