2 * Copyright (c) 2018 Samsung Electronics Co., Ltd. All rights reserved.
4 * Licensed under the Flora License, Version 1.1 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://floralicense.org/license/
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
22 template <typename Task>
24 using lock_t = std::unique_lock<std::mutex>;
28 std::condition_variable _ready;
31 bool try_pop(Task &task)
33 lock_t lock{_mutex, std::try_to_lock};
34 if (!lock || _q.empty()) return false;
35 task = std::move(_q.front());
40 bool try_push(Task &&task)
43 lock_t lock{_mutex, std::try_to_lock};
44 if (!lock) return false;
45 _q.push_back(std::move(task));
63 while (_q.empty() && !_done) _ready.wait(lock);
64 if (_q.empty()) return false;
65 task = std::move(_q.front());
70 void push(Task &&task)
74 _q.push_back(std::move(task));
81 #endif // VTASKQUEUE_H