simple_thread_safe_queue.h
Go to the documentation of this file.
1 // Copyright (c) 2017, The Regents of the University of California
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met:
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above copyright
9 // notice, this list of conditions and the following disclaimer in the
10 // documentation and/or other materials provided with the distribution.
11 // * Neither the name of the University of California nor the
12 // names of its contributors may be used to endorse or promote products
13 // derived from this software without specific prior written permission.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 // ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OF THE UNIVERSITY OF CALIFORNIA
19 // BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 // POSSIBILITY OF SUCH DAMAGE.
26 
27 #ifndef UTIL_SIMPLE_THREAD_SAFE_QUEUE_H_
28 #define UTIL_SIMPLE_THREAD_SAFE_QUEUE_H_
29 
30 #include <condition_variable>
31 #include <chrono>
32 #include <mutex>
33 #include <queue>
34 #include <utility>
35 
36 #include "util/statusor.h"
37 
38 namespace util {
39 
40 template <typename T> class SimpleThreadSafeQueue {
41  private:
42  std::queue<T> queue_;
43  std::mutex mutex_;
44  std::condition_variable condvar_;
45 
46  public:
47  typedef T value_type;
48  template<typename... Args> void push(Args&&... args) {
49  std::unique_lock<std::mutex> lock(mutex_);
50  queue_.push(std::forward<Args>(args)...);
51  lock.unlock();
52  condvar_.notify_one();
53  }
54 
55  size_t size() {
56  std::lock_guard<std::mutex> lock(mutex_);
57  return queue_.size();
58  }
59 
61  std::unique_lock<std::mutex> lock(mutex_);
62  condvar_.wait(lock, [&] { return !queue_.empty(); });
63  T result = std::move(queue_.front());
64  queue_.pop();
65  return std::move(result);
66  }
67 
69  std::unique_lock<std::mutex> lock(mutex_);
70  std::chrono::milliseconds wait_for_duration(wait_ms);
71  condvar_.wait_for(lock, wait_for_duration, [&] { return !queue_.empty(); });
72  if (queue_.size() > 0) {
73  T result = std::move(queue_.front());
74  queue_.pop();
75  return std::move(result);
76  }
77  return util::Status(util::error::UNAVAILABLE, "Size of the queue is 0.");
78  }
79 
81  std::lock_guard<std::mutex> lock(mutex_);
82  if (queue_.size() > 0) {
83  T result = std::move(queue_.front());
84  queue_.pop();
85  return std::move(result);
86  }
87  return util::Status(util::error::UNAVAILABLE, "Size of the queue is 0.");
88  }
89 
90  void clear() {
91  std::lock_guard<std::mutex> lock(mutex_);
92  std::queue<T> tmp;
93  queue_.swap(tmp);
94  }
95 };
96 
97 } // namespace util
98 
99 #endif // UTIL_SIMPLE_THREAD_SAFE_QUEUE_H_
util::StatusOr< T > blocking_pop(int wait_ms)
std::condition_variable condvar_


gcloud_speech
Author(s):
autogenerated on Wed Jun 5 2019 21:24:06