PIThread now wait on _endThread() when starting is really done

This commit is contained in:
2024-12-26 16:01:56 +03:00
parent 6e5556969d
commit 64025e0399

View File

@@ -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)
@@ -733,6 +734,8 @@ int PIThread::priority2System(PIThread::Priority p) {
bool PIThread::_startThread(void * func) {
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";