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

201 lines
4.1 KiB
C++
Raw Normal View History

#pragma once
#include "../acl_cpp_define.hpp"
#include <list>
#include <stdlib.h>
#include "thread_mutex.hpp"
#include "thread_cond.hpp"
#include "noncopyable.hpp"
namespace acl
{
/**
* 线线线
*
*
*
* 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:
2018-07-27 16:26:27 +08:00
/**
*
* @param free_obj {bool} tbox
*
2018-07-27 16:26:27 +08:00
*/
2019-01-02 11:25:17 +08:00
tbox(bool free_obj = true)
2018-07-27 16:26:27 +08:00
: size_(0), free_obj_(free_obj), cond_(&lock_) {}
~tbox(void)
{
2018-07-27 16:26:27 +08:00
clear(free_obj_);
}
/**
*
* @param free_obj {bool} delete
2018-07-27 16:26:27 +08:00
*/
void clear(bool free_obj = false)
{
if (free_obj) {
for (typename std::list<T*>::iterator it =
tbox_.begin(); it != tbox_.end(); ++it) {
delete *it;
}
}
2018-07-27 16:26:27 +08:00
tbox_.clear();
}
/**
*
* @param t {T*}
* @param notify_first {bool} true
*
* @return {bool}
*/
2019-03-13 10:28:51 +08:00
bool push(T* t, bool notify_first = true)
{
if (lock_.lock() == false) {
abort();
}
tbox_.push_back(t);
size_++;
2019-03-13 10:28:51 +08:00
if (notify_first) {
if (cond_.notify() == false) {
abort();
}
if (lock_.unlock() == false) {
abort();
}
} else {
if (lock_.unlock() == false) {
abort();
}
if (cond_.notify() == false) {
abort();
}
}
2019-03-13 10:28:51 +08:00
return true;
}
/**
*
* @param wait_ms {int} >= 0 ()
*
* @param found {bool*}
*
* @return {T*} NULL NULL
* push NULL NULL
* wait_ms
* -1 NULL wait_ms
* 0 NULL found true false
*
*/
2018-07-27 13:26:07 +08:00
T* pop(int wait_ms = -1, bool* found = NULL)
{
long long n = ((long long) wait_ms) * 1000;
2018-07-27 13:26:07 +08:00
bool found_flag;
if (lock_.lock() == false) {
abort();
}
2018-07-29 18:57:47 +08:00
while (true) {
2018-07-27 13:26:07 +08:00
T* t = peek(found_flag);
if (found_flag) {
if (lock_.unlock() == false) {
abort();
}
2018-07-27 13:26:07 +08:00
if (found) {
*found = found_flag;
}
return t;
}
// 注意调用顺序,必须先调用 wait 再判断 wait_ms
2018-07-27 16:26:27 +08:00
if (!cond_.wait(n, true) && wait_ms >= 0) {
if (lock_.unlock() == false) {
abort();
}
2018-07-27 13:26:07 +08:00
if (found) {
*found = false;
}
return NULL;
}
}
}
/**
*
* @return {size_t}
*/
size_t size(void) const
{
return size_;
}
public:
void lock(void)
{
if (lock_.lock() == false) {
abort();
}
}
void unlock(void)
{
if (lock_.unlock() == false) {
abort();
}
}
private:
2018-07-27 16:26:27 +08:00
std::list<T*> tbox_;
size_t size_;
bool free_obj_;
thread_mutex lock_;
thread_cond cond_;
2018-07-27 13:26:07 +08:00
T* peek(bool& found_flag)
{
typename std::list<T*>::iterator it = tbox_.begin();
2018-07-27 13:26:07 +08:00
if (it == tbox_.end()) {
found_flag = false;
return NULL;
2018-07-27 13:26:07 +08:00
}
found_flag = true;
size_--;
T* t = *it;
tbox_.erase(it);
return t;
}
};
} // namespace acl