Ask Your Question

imshow() not working in pthread MacOSX 10.9

asked 2014-03-24 21:12:04 -0500

Marco Leopardi gravatar image

updated 2014-03-24 21:12:49 -0500

Hello guys, here's my problem: on ubuntu 12.04 LTS with OpenCV 2.4.8 the following code works at 100%, in MacOSX (10.9.1), lastest version of Xcode (5.1) and OpenCV 2.4.8 i get a fully blank image and the program hang, if i move the cursor over the blank image i can see it "spinning" and the only way to stop the program is pushing the stop button on the Xcode interface. Another info: imwrite() within a thread works perfectly.

 void* test(void* arg);

 int main( int argc, char **argv )

 Mat I = imread("/Users/Admin/Documents/X-Code/TestOpenCV/coin1.jpg", 1);
 imshow("1", I);

 Mat I2 = imread("/Users/Admin/Documents/X-Code/TestOpenCV/coin2.jpg", 1);

 pthread_t h;
 pthread_create(&h, NULL, test, (void*)&I2);
 pthread_join(h, NULL);

 return 0;

void* test(void* arg){

    imwrite("/Users/Admin/Documents/debug.jpg", *((Mat*)arg)); // WORKS!
    imshow("2", *((Mat*)arg)); // HANGS ON MACOSX

    return NULL;
edit retag flag offensive close merge delete

2 answers

Sort by » oldest newest most voted

answered 2014-03-24 21:22:46 -0500

updated 2014-03-26 20:32:20 -0500

You might need to initialize the visualization window before creating the thread with



Also, inside the thread function you should do:


Instead of

return NULL;

To comply with POSIX threads specifications.

edit flag offensive delete link more


Thanks for your answer, this help but do not solve the problem, because i'm now able to show the image, but if i call waitKey() it hangs like before, so there is still something wrong...

Marco Leopardi gravatar imageMarco Leopardi ( 2014-03-25 06:50:31 -0500 )edit

I have updated my answer, please check it out

Martin Peris gravatar imageMartin Peris ( 2014-03-26 20:29:19 -0500 )edit

answered 2014-03-24 21:59:34 -0500

unxnut gravatar image

imshow is not thread safe. You may want to move it to a point after the thread's termination.

edit flag offensive delete link more


Thanks for your answer, unfortunately i need this for a video stream, i cannot call it after thread termination. Can you propose something that might work? (i repeat in Ubuntu that code work flawlessly)

Marco Leopardi gravatar imageMarco Leopardi ( 2014-03-25 06:23:04 -0500 )edit

Typically, you will synchronize the threads. Perform imshow just after the synchronization.

unxnut gravatar imageunxnut ( 2014-03-25 08:24:45 -0500 )edit

Question Tools


Asked: 2014-03-24 21:12:04 -0500

Seen: 3,670 times

Last updated: Mar 26 '14