6 #ifndef MOTORCORTEX_CORE_UTL_TSQUEUE_H
7 #define MOTORCORTEX_CORE_UTL_TSQUEUE_H
9 #include <condition_variable>
18 void push(T new_value) {
19 std::lock_guard<std::mutex> lk(mut_);
20 data_queue_.push(std::move(new_value));
21 data_cond_.notify_one();
24 void waitAndPop(T& value) {
25 std::unique_lock<std::mutex> lk(mut_);
26 data_cond_.wait(lk, [
this] {
return !data_queue_.empty() || destroy_; });
28 value = std::move(data_queue_.front());
33 std::shared_ptr<T> waitAndPop() {
34 std::unique_lock<std::mutex> lk(mut_);
35 data_cond_.wait(lk, [
this] {
return !data_queue_.empty() || destroy_; });
37 std::shared_ptr<T> res(std::make_shared<T>(std::move(data_queue_.front())));
44 bool tryPop(T& value) {
45 std::lock_guard<std::mutex> lk(mut_);
46 if (data_queue_.empty()) {
49 value = std::move(data_queue_.front());
54 std::shared_ptr<T> tryPop() {
55 std::lock_guard<std::mutex> lk(mut_);
56 if (data_queue_.empty()) {
57 return std::shared_ptr<T>();
59 std::shared_ptr<T> res(std::make_shared<T>(std::move(data_queue_.front())));
65 std::lock_guard<std::mutex> lk(mut_);
66 return data_queue_.empty();
70 std::lock_guard<std::mutex> lk(mut_);
72 data_cond_.notify_all();
76 mutable std::mutex mut_;
77 std::queue<T> data_queue_;
78 std::condition_variable data_cond_;
82 #endif // MOTORCORTEX_CORE_UTL_TSQUEUE_H