Skip to content

Commit

Permalink
Use pthread condition to sync mask calculation with new video frame
Browse files Browse the repository at this point in the history
  • Loading branch information
peckto committed Apr 5, 2021
1 parent a60698b commit 6b8ce1b
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions deepseg.cc
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ typedef struct {
timinginfo_t *pti;
pthread_mutex_t lock_raw;
pthread_mutex_t lock_mask;
pthread_cond_t condition_new_frame;
bool running;
} calcinfo_t;

Expand Down Expand Up @@ -205,6 +206,7 @@ void init_tensorflow(calcinfo_t &info) {

info.lock_raw = PTHREAD_MUTEX_INITIALIZER;
info.lock_mask = PTHREAD_MUTEX_INITIALIZER;
info.condition_new_frame = PTHREAD_COND_INITIALIZER;
info.running = false;
}

Expand All @@ -214,6 +216,7 @@ void calc_mask(calcinfo_t &info) {
// copy frame from shared buffer
cv::Mat raw;
pthread_mutex_lock(&info.lock_raw);
pthread_cond_wait(&info.condition_new_frame, &info.lock_raw); // wait for new frame to arrive
raw = info.raw.clone();
pthread_mutex_unlock(&info.lock_raw);

Expand Down Expand Up @@ -484,6 +487,7 @@ int main(int argc, char* argv[]) {


bool filterActive = true;
calcinfo.running = true;

// mainloop
for(bool running = true; running; ) {
Expand All @@ -503,12 +507,10 @@ int main(int argc, char* argv[]) {
pthread_mutex_lock(&calcinfo.lock_raw);
ti.lockns=timestamp();
calcinfo.raw = raw.clone();
pthread_cond_signal(&calcinfo.condition_new_frame);
pthread_mutex_unlock(&calcinfo.lock_raw);
ti.copyns=timestamp();

// signal ai thread data arrived for the first time
calcinfo.running = true;

if (filterActive) {
pthread_mutex_lock(&calcinfo.lock_mask);
mask = calcinfo.mask.clone();
Expand Down

0 comments on commit 6b8ce1b

Please sign in to comment.