7#include <condition_variable>
20 Task(
const char *name,
int stackSize,
int priority = 1,
int core = -1) {
29 bool create(
const char *name,
int stackSize,
int priority = 1,
38 bool begin(std::function<
void()> process) {
39 if (running_thread.joinable())
return false;
41 terminate_flag =
false;
44 running_thread = std::thread([
this] { this->thread_loop(); });
51 terminate_flag =
true;
53 if (running_thread.joinable()) {
54 if (std::this_thread::get_id() == running_thread.get_id()) {
56 running_thread.detach();
58 running_thread.join();
64 std::lock_guard<std::mutex> lk(mtx);
70 std::lock_guard<std::mutex> lk(mtx);
77 std::thread::id &getTaskHandle() {
return thread_id; }
79 void setReference(
void *r) { ref = r; }
80 void *getReference() {
return ref; }
83 std::thread running_thread;
84 std::thread::id thread_id{};
85 std::function<void()> loop_code = nop;
87 std::atomic<bool> terminate_flag{
false};
89 std::condition_variable cv;
93 std::this_thread::sleep_for(std::chrono::milliseconds(100));
97 thread_id = std::this_thread::get_id();
98 while (!terminate_flag.load()) {
101 std::unique_lock<std::mutex> lk(mtx);
102 cv.wait(lk, [
this] {
return !paused || terminate_flag.load(); });
104 if (terminate_flag.load())
break;