7#include <condition_variable>
21class Task :
public ITask {
23 Task(
const char *name,
int stackSize,
int priority = 1,
int core = -1) {
32 bool create(
const char *name,
int stackSize,
int priority = 1,
41 bool begin(std::function<
void()> process) {
42 if (running_thread.joinable())
return false;
44 terminate_flag =
false;
47 running_thread = std::thread([
this] { this->thread_loop(); });
54 terminate_flag =
true;
56 if (running_thread.joinable()) {
57 if (std::this_thread::get_id() == running_thread.get_id()) {
59 running_thread.detach();
61 running_thread.join();
67 std::lock_guard<std::mutex> lk(mtx);
73 std::lock_guard<std::mutex> lk(mtx);
86 std::thread running_thread;
87 std::thread::id thread_id{};
90 std::atomic<bool> terminate_flag{
false};
92 std::condition_variable cv;
96 std::this_thread::sleep_for(std::chrono::milliseconds(100));
100 thread_id = std::this_thread::get_id();
101 while (!terminate_flag.load()) {
104 std::unique_lock<std::mutex> lk(mtx);
105 cv.wait(lk, [
this] {
return !paused || terminate_flag.load(); });
107 if (terminate_flag.load())
break;