change last logic to PIThreadNotifier

This commit is contained in:
2025-01-15 18:38:50 +03:00
parent 5755d172cd
commit 82cda42e75
3 changed files with 6 additions and 10 deletions

View File

@@ -908,9 +908,7 @@ void PIThread::_endThread() {
PIScopeExitCall ec([this] { PIScopeExitCall ec([this] {
terminating = running_ = false; terminating = running_ = false;
tid_ = -1; tid_ = -1;
state_mutex.lock(); state_notifier.notify();
state_var.notifyAll();
state_mutex.unlock();
}); });
// PICout(PICoutManipulators::DefaultControls) << "thread" << this << "stop" << "..."; // PICout(PICoutManipulators::DefaultControls) << "thread" << this << "stop" << "...";
stopped(); stopped();
@@ -1078,9 +1076,7 @@ void PIThread::setThreadName() {
bool PIThread::_waitForFinish(PISystemTime max_tm) { bool PIThread::_waitForFinish(PISystemTime max_tm) {
if (!running_) return true; if (!running_) return true;
state_mutex.lock(); state_notifier.waitFor(max_tm);
state_var.waitFor(state_mutex, max_tm);
state_mutex.unlock();
if (!running_) return true; if (!running_) return true;
#ifdef WINDOWS #ifdef WINDOWS
if (!isExists(PRIVATE->thread)) { if (!isExists(PRIVATE->thread)) {

View File

@@ -26,10 +26,10 @@
#ifndef PITHREAD_H #ifndef PITHREAD_H
#define PITHREAD_H #define PITHREAD_H
#include "piconditionvar.h"
#include "piinit.h" #include "piinit.h"
#include "pimutex.h" #include "pimutex.h"
#include "piobject.h" #include "piobject.h"
#include "pithreadnotifier.h"
class PIThread; class PIThread;
@@ -287,11 +287,11 @@ protected:
PISystemTime delay_; PISystemTime delay_;
llong tid_ = -1; llong tid_ = -1;
void * data_ = nullptr; void * data_ = nullptr;
mutable PIMutex thread_mutex, state_mutex; mutable PIMutex thread_mutex;
PITimeMeasurer tmf_, tms_, tmr_; PITimeMeasurer tmf_, tms_, tmr_;
PIThread::Priority priority_ = piNormal; PIThread::Priority priority_ = piNormal;
ThreadFunc ret_func = nullptr; ThreadFunc ret_func = nullptr;
PIConditionVariable state_var; PIThreadNotifier state_notifier;
PRIVATE_DECLARATION(PIP_EXPORT) PRIVATE_DECLARATION(PIP_EXPORT)
private: private:

View File

@@ -118,7 +118,7 @@ void PIThreadNotifier::wait() {
bool PIThreadNotifier::waitFor(PISystemTime timeout) { bool PIThreadNotifier::waitFor(PISystemTime timeout) {
bool ret = false; bool ret = false;
m.lock(); m.lock();
v.waitFor(m, timeout); if (cnt == 0) v.waitFor(m, timeout);
if (cnt > 0) { if (cnt > 0) {
cnt--; cnt--;
ret = true; ret = true;