Skip to content

Commit

Permalink
fixed mutex problem
Browse files Browse the repository at this point in the history
  • Loading branch information
internaut committed Jul 7, 2014
1 parent 09f8a40 commit dbbd169
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 25 deletions.
14 changes: 3 additions & 11 deletions detect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "detect.h"

#include "tools.h"
#include "threading.h"

using namespace ocv_ar;

Expand Down Expand Up @@ -229,12 +230,12 @@ void Detect::processFrame(bool enableMutex) {
performThreshold();
findContours();

if (enableMutex) lockMarkers();
if (enableMutex) Threading::mutexLock();

findMarkerCandidates();
identifyMarkers();

if (enableMutex) unlockMarkers();
// if (enableMutex) unlockMarkers();
}

void Detect::estimateMarkersPoses() {
Expand Down Expand Up @@ -267,15 +268,6 @@ cv::Mat *Detect::getOutputFrame() const {

#pragma mark private methods

void Detect::lockMarkers() {
while (markersLocked) {};
markersLocked = true;
}

void Detect::unlockMarkers() {
markersLocked = false;
}

void Detect::preprocess() {
// downscale the image

Expand Down
5 changes: 0 additions & 5 deletions detect.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,6 @@ class Detect {
*/
~Detect();

void lockMarkers();
void unlockMarkers();

/**
* Prepare for input frames of size <frameW> x <frameH> with <frameChan>
* color channels. Specify a conversion type <cvtType> for grayscale conversion of
Expand Down Expand Up @@ -201,8 +198,6 @@ class Detect {

bool prepared; // detector is prepared (<prepare()> called)?

bool markersLocked; // mutex variable to lock markers vector access

int inputFrameCvtType; // color conversion type for input frames

FrameProcLevel outFrameProcLvl; // frame output processing level
Expand Down
28 changes: 28 additions & 0 deletions threading.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* ocv_ar - OpenCV based Augmented Reality library
*
* Helper function class for threading -- implementation file.
*
* Author: Markus Konrad <konrad@htw-berlin.de>, July 2014.
* INKA Research Group, HTW Berlin - http://inka.htw-berlin.de/
*
* See LICENSE for license.
*/

#include "threading.h"

using namespace ocv_ar;

pthread_mutex_t Threading::mutex;

void Threading::init() {
pthread_mutex_init (&mutex, NULL);
}

void Threading::mutexLock() {
pthread_mutex_lock(&mutex);
}

void Threading::mutexUnlock() {
pthread_mutex_unlock(&mutex);
}
32 changes: 32 additions & 0 deletions threading.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* ocv_ar - OpenCV based Augmented Reality library
*
* Helper function class for threading -- header file.
*
* Author: Markus Konrad <konrad@htw-berlin.de>, July 2014.
* INKA Research Group, HTW Berlin - http://inka.htw-berlin.de/
*
* See LICENSE for license.
*/

#ifndef OCV_AR_THREADING_H
#define OCV_AR_THREADING_H

#include <pthread.h>

namespace ocv_ar {

class Threading {
public:
static void init();

static void mutexLock();
static void mutexUnlock();

private:
static pthread_mutex_t mutex;
};

}

#endif
11 changes: 4 additions & 7 deletions track.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "track.h"

#include "tools.h"
#include "threading.h"

using namespace ocv_ar;

Expand All @@ -29,30 +30,26 @@ void Track::detect(const cv::Mat *frame) {
// lockMarkers(); // lock markers map

// detect and identify the markers
detector->processFrame(true);
detector->processFrame(true); // will call Threading::mutexLock at the correct spot

// correct the vertices of the found markers
lockMarkers();
detector->lockMarkers();
correctMarkerVertexOrder(detector->getMarkers());

// estimate the markers' 3D poses
detector->estimateMarkersPoses();

detector->unlockMarkers();
unlockMarkers();

// unlockMarkers(); // lock markers map
detectionRunning = false;
}

void Track::lockMarkers() {
while (markersLocked) {};
markersLocked = true;
Threading::mutexLock();
}

void Track::unlockMarkers() {
markersLocked = false;
Threading::mutexUnlock();
}

void Track::update() {
Expand Down
2 changes: 0 additions & 2 deletions track.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ typedef std::pair<int, Marker> MarkerMapPair;
class Track {
public:
Track(Detect *detectorPtr) : detector(detectorPtr),
markersLocked(false),
detectionRunning(false)
{};

Expand All @@ -58,7 +57,6 @@ class Track {


MarkerMap markers;
bool markersLocked;

bool detectionRunning;

Expand Down

0 comments on commit dbbd169

Please sign in to comment.