From 4994d0bf66e1a43bbbe129bf8e63dc8ad7144193 Mon Sep 17 00:00:00 2001 From: peri4 Date: Thu, 10 Nov 2022 14:11:40 +0300 Subject: [PATCH] condvar fixes --- libs/main/core/piobject.cpp | 4 +--- libs/main/thread/pitimer.cpp | 2 -- main.cpp | 16 ++++++++++++++-- 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/libs/main/core/piobject.cpp b/libs/main/core/piobject.cpp index 95f345a1..dd4eed43 100644 --- a/libs/main/core/piobject.cpp +++ b/libs/main/core/piobject.cpp @@ -839,7 +839,7 @@ PIObject::Deleter::Deleter() { PRIVATE->thread.setSlot([this](){ PIVector oq; PRIVATE->thread.lock(); - PRIVATE->cond_var.wait(PRIVATE->thread.mutex()); + if (PRIVATE->obj_queue.isEmpty()) PRIVATE->cond_var.wait(PRIVATE->thread.mutex()); oq.swap(PRIVATE->obj_queue); PRIVATE->thread.unlock(); for (PIObject * o : oq) deleteObject(o); @@ -851,9 +851,7 @@ PIObject::Deleter::Deleter() { PIObject::Deleter::~Deleter() { //piCout << "~Deleter ..."; PRIVATE->thread.stop(); - PRIVATE->thread.mutex().lock(); PRIVATE->cond_var.notifyAll(); - PRIVATE->thread.mutex().unlock(); PRIVATE->thread.waitForFinish(); for (PIObject * o : PRIVATE->obj_queue) deleteObject(o); //piCout << "~Deleter ok"; diff --git a/libs/main/thread/pitimer.cpp b/libs/main/thread/pitimer.cpp index 45aa73bc..a06889f2 100644 --- a/libs/main/thread/pitimer.cpp +++ b/libs/main/thread/pitimer.cpp @@ -278,9 +278,7 @@ bool _PITimerImp_Thread::startTimer(double interval_ms) { bool _PITimerImp_Thread::stopTimer(bool wait) { thread_.stop(); - thread_.mutex().lock(); event.notifyAll(); - thread_.mutex().unlock(); if (wait) return thread_.waitForFinish(); // if (wait) // if (!thread_.waitForFinish(10)) diff --git a/main.cpp b/main.cpp index 5ace916b..2b673b70 100644 --- a/main.cpp +++ b/main.cpp @@ -87,7 +87,7 @@ void phase(const char * msg) { } int main(int argc, char * argv[]) { - piCout << "main"; + /*piCout << "main"; PITimer timer; timer.setSlot([](){ @@ -101,7 +101,19 @@ int main(int argc, char * argv[]) { PITimeMeasurer tm; timer.stop(); double tm_ms = tm.elapsed_m(); - piCout << "stop took" << tm_ms; + piCout << "stop took" << tm_ms;*/ + + PIWaitEvent event; + event.create(); + tm.reset(); + PIThread::runOnce([&event](){ + //piMSleep(100); + piCout << "interrupt"; + //event.interrupt(); + }); + event.sleep(2010000); + double tm_ms = tm.elapsed_m(); + piCout << "waited for" << tm_ms; return 0; /*for (int i = 0; i < count; ++i)