fixed one bug in tbox

This commit is contained in:
zsx 2018-07-27 16:26:27 +08:00
parent 2843c3197d
commit 0da7b9bd8a

View File

@ -42,16 +42,34 @@ template<typename T>
class tbox : public noncopyable class tbox : public noncopyable
{ {
public: public:
tbox(void) : size_(0) , cond_(&lock_) {} /**
*
* @param free_obj {bool} tbox
*
*/
tbox(bool free_obj = false)
: size_(0), free_obj_(free_obj), cond_(&lock_) {}
~tbox(void) ~tbox(void)
{ {
for (typename std::list<T*>::iterator it = tbox_.begin(); clear(free_obj_);
it != tbox_.end(); ++it) }
/**
*
* @param free_obj {bool} delete
*/
void clear(bool free_obj = false)
{ {
if (free_obj) {
for (typename std::list<T*>::iterator it =
tbox_.begin(); it != tbox_.end(); ++it) {
delete *it; delete *it;
} }
} }
tbox_.clear();
}
/** /**
* *
@ -95,7 +113,8 @@ public:
return t; return t;
} }
if (wait_ms >= 0 && !cond_.wait(n, true)) { // 注意调用顺序,必须先调用 wait 再判断 wait_ms
if (!cond_.wait(n, true) && wait_ms >= 0) {
lock_.unlock(); lock_.unlock();
if (found) { if (found) {
*found = false; *found = false;
@ -128,6 +147,7 @@ public:
private: private:
std::list<T*> tbox_; std::list<T*> tbox_;
size_t size_; size_t size_;
bool free_obj_;
thread_mutex lock_; thread_mutex lock_;
thread_cond cond_; thread_cond cond_;