2 * Copyright (c) 2018 Samsung Electronics Co., Ltd. All rights reserved.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 template <typename Task>
26 using lock_t = std::unique_lock<std::mutex>;
30 std::condition_variable _ready;
33 bool try_pop(Task &task)
35 lock_t lock{_mutex, std::try_to_lock};
36 if (!lock || _q.empty()) return false;
37 task = std::move(_q.front());
42 bool try_push(Task &&task)
45 lock_t lock{_mutex, std::try_to_lock};
46 if (!lock) return false;
47 _q.push_back(std::move(task));
65 while (_q.empty() && !_done) _ready.wait(lock);
66 if (_q.empty()) return false;
67 task = std::move(_q.front());
72 void push(Task &&task)
76 _q.push_back(std::move(task));
83 #endif // VTASKQUEUE_H