Refactor the use of multiple threads for streams

This commit is contained in:
jfmcarreira committed 2021-09-12 23:43:11 +01:00
1 parent 9a63a57458
commit de80c85596
8 files changed
+139 -91

No files matched your search

+26 -6
View File
@@ -30,6 +30,8 @@
#include <QFileInfo>
#include <QMutex>
#include <QThread>
#include <chrono>
#include <iostream>
#include <memory>
#include <optional>
@@ -41,6 +43,7 @@ ResourceWorker::ResourceWorker( std::shared_ptr<CalypStream> stream )
void ResourceWorker::stop()
{
m_bStop = true;
wake();
wait();
}
@@ -53,16 +56,23 @@ void ResourceWorker::wake()
void ResourceWorker::run()
{
m_bStop = false;
// Loop forever
while( !m_bStop )
{
//auto start = std::chrono::steady_clock::now();
m_pcStream->readNextFrameFillRGBBuffer();
if( !m_pcStream->hasWritingSlot() )
{
m_Mutex.lock();
m_ResourceIdle.wait( &m_Mutex );
m_Mutex.unlock();
}
// auto end = std::chrono::steady_clock::now();
// std::cout << "Elapsed time reading and processing a frame: "
// << std::chrono::duration_cast<std::chrono::milliseconds>( 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() )
{
// Wait here
@@ -123,6 +133,16 @@ void ResourceHandle::removeResource( std::size_t id )
m_apcStreamResourcesList[id] = nullptr;
}
void ResourceHandle::stopResourceWorker( std::size_t id )
{
if( !m_apcStreamResourcesWorkersList.contains( id ) )
{
assert( false );
return;
}
m_apcStreamResourcesWorkersList[id]->stop();
}
void ResourceHandle::startResourceWorker( std::size_t id )
{
if( !m_apcStreamResourcesWorkersList.contains( id ) )