diff --git a/app/ResourceHandle.cpp b/app/ResourceHandle.cpp index 9016610..7794620 100644 --- a/app/ResourceHandle.cpp +++ b/app/ResourceHandle.cpp @@ -36,13 +36,14 @@ #include ResourceWorker::ResourceWorker( std::shared_ptr stream ) + : m_pcStream{ stream } { - m_pcStream = stream; + m_bStop.clear(); } void ResourceWorker::stop() { - m_bStop = true; + m_bStop.test_and_set(); wake(); wait(); } @@ -56,26 +57,23 @@ void ResourceWorker::wake() void ResourceWorker::run() { - m_bStop = false; + m_bStop.clear(); // Loop forever - while( !m_bStop ) + while( !m_bStop.test() ) { - //auto start = std::chrono::steady_clock::now(); + // auto start = std::chrono::steady_clock::now(); m_pcStream->readNextFrameFillRGBBuffer(); // auto end = std::chrono::steady_clock::now(); // std::cout << "Elapsed time reading and processing a frame: " // << std::chrono::duration_cast( end - start ).count() // << " ms" << std::endl; - // if( !m_pcStream->hasWritingSlot() ) - // { - // m_Mutex.lock(); - // m_ResourceIdle.wait( &m_Mutex ); - // m_Mutex.unlock(); - // } - while( !m_bStop && !m_pcStream->hasWritingSlot() ) + while( !m_bStop.test() && !m_pcStream->hasWritingSlot() ) { // Wait here + m_Mutex.lock(); + m_ResourceIdle.wait( &m_Mutex ); + m_Mutex.unlock(); } } } @@ -150,6 +148,10 @@ void ResourceHandle::startResourceWorker( std::size_t id ) assert( false ); return; } + if( m_apcStreamResourcesList[id]->getFrameNum() < 2 ) + { + return; // No work to be done! + } m_apcStreamResourcesWorkersList[id]->setObjectName( "RW-" + QFileInfo( QString::fromStdString( m_apcStreamResourcesList[id]->getFileName() ) ).completeBaseName() ); diff --git a/app/ResourceHandle.h b/app/ResourceHandle.h index 2af3812..df8302c 100644 --- a/app/ResourceHandle.h +++ b/app/ResourceHandle.h @@ -29,6 +29,7 @@ #include #include #include +#include #include "CommonDefs.h" #include "lib/CalypStream.h" @@ -39,7 +40,7 @@ private: QMutex m_Mutex; QWaitCondition m_ResourceIdle; std::shared_ptr m_pcStream; - bool m_bStop{ false }; + std::atomic_flag m_bStop; public: ResourceWorker( std::shared_ptr stream );