Overte C++ Documentation
TaskQueue.h
1 //
2 // Created by Bradley Austin Davis on 2018/11/15
3 // Copyright 2013-2018 High Fidelity, Inc.
4 //
5 // Distributed under the Apache License, Version 2.0.
6 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
7 //
8 #pragma once
9 
10 #include <mutex>
11 #include <functional>
12 
13 namespace ovr {
14 
15 using Mutex = std::mutex;
16 using Condition = std::condition_variable;
17 using Lock = std::unique_lock<Mutex>;
18 using Task = std::function<void()>;
19 using LockTask = std::function<void(Lock& lock)>;
20 
21 class TaskQueue {
22 public:
23  // Execute a task on another thread
24  void submitTaskBlocking(const Task& task);
25  void submitTaskBlocking(Lock& lock, const Task& task);
26  void pollTask();
27 
28  void withLock(const Task& task);
29  void withLockConditional(const LockTask& task);
30 private:
31  Mutex _mutex;
32  Task _task;
33  bool _taskPending{ false };
34  Condition _taskCondition;
35 };
36 
37 }
38 
39 
40 
41 
42