Jump to content
  • Advertisement
Sign in to follow this  

open() call failing within a thread?

This topic is 5158 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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;

  printf( "%s\n", data->deviceName );
  rc = pthread_mutex_lock( &a_mutex );
  if( !rc )
      printf( "Mutex locked for device open\n" );
      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" );
  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 );

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!

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!