diff --git a/libs/main/thread/pithread.cpp b/libs/main/thread/pithread.cpp index 53554c95..c79e3b3f 100644 --- a/libs/main/thread/pithread.cpp +++ b/libs/main/thread/pithread.cpp @@ -533,6 +533,7 @@ PRIVATE_DEFINITION_START(PIThread) pthread_t thread = 0; sched_param sparam; #endif + std::atomic_bool starting = {false}; PRIVATE_DEFINITION_END(PIThread) @@ -731,8 +732,10 @@ int PIThread::priority2System(PIThread::Priority p) { bool PIThread::_startThread(void * func) { - terminating = false; - running_ = true; + terminating = false; + running_ = true; + PRIVATE->starting = true; + PIScopeExitCall ec([this] { PRIVATE->starting = false; }); #ifdef FREERTOS @@ -754,6 +757,7 @@ bool PIThread::_startThread(void * func) { # else PRIVATE->thread = CreateThread(0, 0, (LPTHREAD_START_ROUTINE)func, this, 0, 0); # endif + // piCout << "started" << PRIVATE->thread; if (PRIVATE->thread != 0) { setPriority(priority_); return true; @@ -768,6 +772,7 @@ bool PIThread::_startThread(void * func) { // PICout(PICoutManipulators::DefaultControls) << "pthread_create" << PRIVATE->thread; pthread_attr_destroy(&attr); if (ret == 0) { + // if (name().isNotEmpty()) { PIString tname = name().simplified(); tname.elide(15, 0.4f).resize(15, PIChar('\0')); PIByteArray tn_data = tname.toAscii(); @@ -780,6 +785,7 @@ bool PIThread::_startThread(void * func) { pthread_setname_np(PRIVATE->thread, (const char *)tn_data.data()); # endif setPriority(priority_); + // } return true; } @@ -928,7 +934,10 @@ void PIThread::_endThread() { PIScopeExitCall ec([this] { terminating = running_ = false; tid_ = -1; + PRIVATE->thread = 0; }); + while (PRIVATE->starting) + piMinSleep(); // PICout(PICoutManipulators::DefaultControls) << "thread" << this << "stop" << "..."; stopped(); // PICout(PICoutManipulators::DefaultControls) << "thread" << this << "stop" << "ok";