From 0cf139bf8ae4f23e0a535957c1565bbf20d28e88 Mon Sep 17 00:00:00 2001 From: gabime Date: Fri, 31 Jan 2014 22:52:07 +0200 Subject: [PATCH] Universal ref support --- include/c11log/details/blocking_queue.h | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/include/c11log/details/blocking_queue.h b/include/c11log/details/blocking_queue.h index 2db2df85..bc61554e 100644 --- a/include/c11log/details/blocking_queue.h +++ b/include/c11log/details/blocking_queue.h @@ -37,8 +37,8 @@ public: // Push copy of item into the back of the queue. // If the queue is full, block the calling thread util there is room or timeout have passed. // Return: false on timeout, true on successful push. - template - bool push(const T& item, const std::chrono::duration& timeout) + template + bool push(TT&& item, const std::chrono::duration& timeout) { std::unique_lock ul(mutex_); if (q_.size() >= max_size_) @@ -46,7 +46,7 @@ public: if (!item_popped_cond_.wait_until(ul, clock::now() + timeout, [this]() { return this->q_.size() < this->max_size_; })) return false; } - q_.push(item); + q_.push(std::forward(item)); if (q_.size() <= 1) { ul.unlock(); //So the notified thread will have better chance to accuire the lock immediatly.. @@ -57,9 +57,10 @@ public: // Push copy of item into the back of the queue. // If the queue is full, block the calling thread until there is room. - void push(const T& item) + template + void push(TT&& item) { - while (!push(item, one_hour)); + while (!push(std::forward(item), one_hour)); } // Pop a copy of the front item in the queue into the given item ref. @@ -104,7 +105,7 @@ private: std::mutex mutex_; std::condition_variable item_pushed_cond_; std::condition_variable item_popped_cond_; - static constexpr auto one_hour = std::chrono::hours(1); + const std::chrono::hours one_hour{1}; }; }