-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTimedTaskQueue.h
57 lines (39 loc) · 1.73 KB
/
TimedTaskQueue.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#pragma once
#include "cinder/app/App.h"
#include "cinder/app/RendererGl.h"
#include "cinder/gl/gl.h"
#include <queue>
#include <mutex>
namespace bluecadet {
namespace utils {
typedef std::shared_ptr<class TimedTaskQueue> TaskQueueRef;
class TimedTaskQueue {
public:
typedef std::function<void(void)> TaskFn;
TimedTaskQueue(const bool autoStart = true, double maxExecutionTime = -1.0);
~TimedTaskQueue();
//! Begins processing tasks with max execution time spread out over multiple frames.
void start();
//! Ends task processing; Does not clear any tasks.
void stop();
//! Clears all pending tasks. This method is thread-safe and blocking.
void clear();
//! Adds a task to the queue. Tasks are executed first-in-first-out. This method is thread-safe and blocking.
void add(TaskFn task);
//! Can be used to explicitly process all tasks regardless of how long they take. This method is thread-safe and blocking.
void processAllTasks();
//! Number of currently pending tasks. Includes the currently running task.
int getNumPendingTasks() const { return (int)mPendingTasks.size(); };
//! Will run tasks on each update call until max execution time is reached. Default is -1, which means infinite execution time.
double getMaxExecutionTime() const { return mMaxExecutionTime; }
void setMaxExecutionTime(const double value) { mMaxExecutionTime = value; }
protected:
//! Called automatically when started, but can be called explicitly to process tasks separately. This method is thread-safe and blocking.
void processTasks(double maxExecutionTime);
ci::signals::Connection mMainLoopConnection;
std::mutex mTaskMutex;
std::deque <TaskFn>mPendingTasks;
double mMaxExecutionTime;
};
} // utils namespace
} // bluecadet namespace