open() call failing within a thread?

Started by
-1 comments, last by Bacardi34 19 years, 7 months ago
Hi, im trying to use threads to read from multiple web cams attached to my computer so that i can make sure the frames are synced up as close as possible, and to speed up the rate of capture. I have the thread written, but for some reason, when i try and make the open() call on the device, the thread just stops running and i have no idea why. If i try and do the open call outside the thread, then the devices are opened ok, and when i pass them into the thread and try to do a read call inside the thread, the thread stops running. I know the cameras work and are readable because i have a version of the program that does not use threading and it reads from the cams fine. Can devices be read from within a thread, or am i missing something here? Here is all the code for the thread function.

static pthread_mutex_t a_mutex = PTHREAD_MUTEX_INITIALIZER;


struct thread_data
{
  char deviceName[40];
  int device;
  char name[200];
  int maxFrames;
  int xOffset;
  int yOffset;
  char experiment;
  char nopng;
  int cam;
  char isbottom;
  pthread_mutex_t the_mutex;
};

void * cam_read( void * theInfo )
{
  int width, height, depth;
  int i, result;
  unsigned char * image;
  struct thread_data *data;
  data = (struct thread_data *)theInfo;

  char prefix[200];
  int rc;

  fflush(stdout);
  printf( "%s\n", data->deviceName );
  rc = pthread_mutex_lock( &a_mutex );
  if( !rc )
    {
      printf( "Mutex locked for device open\n" );
    }
  else
    {
      printf( "Cant lock mutex for device open\n" );
    }
  data->device = open( data->deviceName, O_RDWR);
  //data->device = open_camera( data->deviceName, a_mutex );  
  if( !rc )
    {
      printf( "Mutex unlocked for device open\n" );
    }
  fflush(stdout);
  printf( "hi %d\n", data->device );
  

  for( i = 0; i < data->maxFrames; i++ )
    {
      printf( "%s %i\n", "READ FRAME:", i );
      // read the cam image
      result = qcam_read( data->device, &width, &height, &depth,
			  &image, a_mutex );
      printf( "%d\n", result );
      if( !result )	
	{
	  printf( "%s %i\n", "BLEAH:", i );
	  get_actual_image( image, width, height, depth,
			    data->xOffset, data->yOffset, data->isbottom, 1 );
	}
      
      if( !data->nopng )
	{
	  if( !data->experiment )
	    {
	      sprintf( prefix, "../var/%s_%d/%s_%d %03d.png", data->name, data->cam, data->name, data->cam, i );
	    }
	  else if ( data->experiment )
	    {
	      sprintf( prefix, "../var/experiments/image_sequences/%s/%s_%d/%s_%d %03d.png", data->name, 
		       data->name, data->cam, data->name, 
		       data->cam, i );
	    }
	  // Ends up looknig like "dirname_cam/frame.png"
	  png_write_qcam( prefix, width, height, i, image, data->isbottom );
	  //fprintf( stdout, "%s\n", prefix );
	}
      free( image );
    }
  
  close_camera( data->device );
  //pthread_exit(0);
}

the output is as follows when i run the program (with 2 cameras). "/dev/video0 Mutex locked for device open /dev/video1" then the program ends. Any ideas? Thanks in advance!

This topic is closed to new replies.

Advertisement