Mat Data in Multithreading

asked 2016-04-16 05:11:55 -0600

mark92m gravatar image

In my code , I use sockets to offload processing of a Mat Image onto another machine, and wait for the result to be read back. So in end up with resulting the Mat Image.

I use fork() to create a child process to take care of the sending and receiving of this Mat image.

Mat image = imread( "image_source" );
Mat result;

fork()  
if( child_process ){

      // OVER SIMPLIFYING:
      open socket();
      send( image.data );
      receive( result.data );
}      

if( parent_process ){ 
    wait();
    imshow(  result ) ;
}

The socket programming works fine and the child thread gets the correct information (verified). Obviously I need to access the resulting image from the parent thread and NOT the child thread only.

The parent thread keeps wait indefinitely, as Mat result is never populated.

Do I need to use some form of shared memory ? Or is that bad practice when using threads ?

edit retag flag offensive close merge delete

Comments

1

This is unrelated to OpenCV. If you want to use multithreaded code, you better start by learning about how to use it properly: threads, mutexes, condition variables and signals, etc. Shared memory is fine as long as you take care...

LorenaGdL gravatar imageLorenaGdL ( 2016-04-16 05:19:46 -0600 )edit
2

fork() works on its own copy of the parent env. (you probably want threads instead of a fork)

berak gravatar imageberak ( 2016-04-16 05:21:29 -0600 )edit