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 bool isEnded() {
return terminate_flag.load(); }
90 std::thread running_thread;
91 std::thread::id thread_id{};
94 std::atomic<bool> terminate_flag{
false};
96 std::condition_variable cv;
100 std::this_thread::sleep_for(std::chrono::milliseconds(100));
104 thread_id = std::this_thread::get_id();
105 while (!terminate_flag.load()) {
108 std::unique_lock<std::mutex> lk(mtx);
109 cv.wait(lk, [
this] {
return !paused || terminate_flag.load(); });
111 if (terminate_flag.load())
break;