ArmedCoder 0 Newbie Poster

The thing is it does not even get to the point where it unlocks the mutex. if i comment out the mutex stuff, and put a print before and after the open() call, it will print before, but not after. its just dying on the open().

Here is the qcam_read()

int qcam_read( int device, int *imgwidth, int * imgheight, int * imgdepth,	
       unsigned char ** image, pthread_mutex_t the_mutex) {

  int rc;
  ioctl(device, VIDIOCGCAP, &vidcap);
  ioctl(device, VIDIOCGWIN, &vidwin);
  ioctl(device, VIDIOCGPICT, &vidpic);
  *imgwidth= vidwin.width;
  *imgheight= vidwin.height;
  *imgdepth= vidpic.depth/8;
  *image = malloc(vidcap.maxwidth * vidcap.maxheight * 3);
  rc = pthread_mutex_lock(&a_mutex);
  printf( "device %d\twidth %d\theight %d\n", device, vidcap.maxwidth, vidcap.maxheight );
  printf( "%x\n", *image );
  if( !rc )
    {
      printf( "Mutex locked in qcam_read\n" );
    }
  else
    {
      printf( "Cant lock mutex in cam_read\n" );
    }  
  read(device, *image, (vidcap.maxwidth * vidcap.maxheight * 3));
  rc = pthread_mutex_unlock(&a_mutex);
  if( !rc )
    {
      printf( "Mutex unlocked in qcam_read\n" );
    }  

  printf( "Qcam_read %d\n",  device );
  return 1;
}

Thx for you help :)

ArmedCoder 0 Newbie Poster

What's in the "ifqcam.h" file? Did you write the qcam_ functions? If so, maybe the problem is in that code.

the only code in that file im using is the open_camera() and the qcam_read() functions.

int open_camera(const char *devicename, pthread_mutex_t mutex)
{
  int rc;
  rc = pthread_mutex_lock( &a_mutex );
  if( !rc )
    {
      printf( "Mutex locked in cam_open\n" );
    }  
  else
    {
      printf( "Cant lock mutex in cam_open\n" );
    }
  int device = open(devicename, O_RDWR);
  pthread_mutex_unlock( &a_mutex );
  if( !rc )
    {
      printf( "Mutex unlocked in cam_open\n" );
    }  
  if(device <= 0)
    {
      printf("Device %s couldn't be opened\n", devicename);
      return 0;
    }
  //return 1;
  return device;
}

and the qcam_read function the thread never even makes it to at all.

ArmedCoder 0 Newbie Poster

"In order to create a mutex, we first need to declare a variable of type pthread_mutex_t, and then initialize it. The simplest way it by assigning it the PTHREAD_MUTEX_INITIALIZER constant. So we'll use a code that looks something like this:

pthread_mutex_t a_mutex = PTHREAD_MUTEX_INITIALIZER;"

I have that at the top of my program. so its initialized and ready to go.

ArmedCoder 0 Newbie Poster

The open does not need a mutex, i just threw it in there to see if it made a difference.
Also, when the program is run with trying to only access one camera the problem still appears. And yes, you can run two programs that both access cams at the same time.

ArmedCoder 0 Newbie Poster

Hi, im just learning about multithreading for a program i am writing that ne
eds to read from multiple camreas attached to the computer at the same time.

I know how to create threads and pass around info, and i know a bit about mu
texes but this is not where the problem resides.
Every time the thread goes to open the device at /dev/video* the thread just
stops running as far as i can see.
i put printf's all over the place and it prints right before the open call o
n the camera, then the next thread kicks in and does the same thing.
I know the camreas work cuse when i go back to the non-multithreaded code, e
verything works just fine.
Also if i try the open call outside the thread, then pass the camrea into th
e thread, they open fine but when the read() call comes, then the thread die
s in the same way.
Does anyone have any ideas whats going on?
Here is the code for the program.
Thanks in advance

#include "ifqcam.h"
#include "error.h"
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>

#include <sys/ioctl.h>
#include <sys/mman.h>
#include <unistd.h>
#include <fcntl.h>
#include <linux/videodev.h>

#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xos.h>
#include <X11/Xatom.h>

#define SETWIDTH 320
#define SETHEIGHT 240
#define SETDEPTH 3
#define SETFOV 0.1
#define SETZDIST 50
#define WINDOWWIDTH 980
#define WINDOWHEIGHT 480
#define MAXVIEWS 15


// Globals that …