Fixed race condition bug on multithreading

This commit is contained in:
Matt Hill
2014-05-04 14:07:57 -05:00
parent 0bf3f674ee
commit d6c79299fb
2 changed files with 11 additions and 18 deletions

View File

@@ -159,18 +159,15 @@ void plateAnalysisThread(void* arg)
int loop_count = 0;
while (true)
{
if (dispatcher->hasPlate() == false)
PlateRegion plateRegion;
if (dispatcher->nextPlate(&plateRegion) == false)
break;
if (dispatcher->config->debugGeneral)
cout << "Thread: " << tthread::this_thread::get_id() << " loop " << ++loop_count << endl;
// Get a single plate region from the queue
PlateRegion plateRegion = dispatcher->nextPlate();
Mat img = dispatcher->getImageCopy();
timespec platestarttime;
getTime(&platestarttime);

View File

@@ -109,22 +109,18 @@ class PlateDispatcher
return img;
}
bool hasPlate()
{
bool plateAvailable;
mMutex.lock();
plateAvailable = plateRegions.size() > 0;
mMutex.unlock();
return plateAvailable;
}
PlateRegion nextPlate()
bool nextPlate(PlateRegion* plateRegion)
{
tthread::lock_guard<tthread::mutex> guard(mMutex);
PlateRegion plateRegion = plateRegions[plateRegions.size() - 1];
if (plateRegions.size() == 0)
return false;
*plateRegion = plateRegions[plateRegions.size() - 1];
plateRegions.pop_back();
return plateRegion;
return true;
}
void appendPlate(PlateRegion plate)