Multithreading problem

Reply

Join Date: Aug 2004
Posts: 5
Reputation: ArmedCoder is an unknown quantity at this point 
Solved Threads: 0
ArmedCoder ArmedCoder is offline Offline
Newbie Poster

Multithreading problem

 
0
  #1
Aug 26th, 2004
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
  1. #include "ifqcam.h"
  2. #include "error.h"
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <pthread.h>
  6.  
  7. #include <sys/ioctl.h>
  8. #include <sys/mman.h>
  9. #include <unistd.h>
  10. #include <fcntl.h>
  11. #include <linux/videodev.h>
  12.  
  13. #include <X11/Xlib.h>
  14. #include <X11/Xutil.h>
  15. #include <X11/Xos.h>
  16. #include <X11/Xatom.h>
  17.  
  18. #define SETWIDTH 320
  19. #define SETHEIGHT 240
  20. #define SETDEPTH 3
  21. #define SETFOV 0.1
  22. #define SETZDIST 50
  23. #define WINDOWWIDTH 980
  24. #define WINDOWHEIGHT 480
  25. #define MAXVIEWS 15
  26.  
  27.  
  28. // Globals that all files can see
  29. int imgwidth = SETWIDTH;
  30. int imgheight = SETHEIGHT;
  31. int imgdepth = SETDEPTH;
  32.  
  33.  
  34. extern Display *display;
  35. extern Window win;
  36.  
  37. typedef unsigned char * charp;
  38.  
  39. pthread_mutex_t a_mutex = PTHREAD_MUTEX_INITIALIZER;
  40.  
  41.  
  42. struct thread_data
  43. {
  44. char deviceName[40];
  45. int device;
  46. char name[200];
  47. int maxFrames;
  48. int xOffset;
  49. int yOffset;
  50. char experiment;
  51. char nopng;
  52. int cam;
  53. char isbottom;
  54. pthread_mutex_t the_mutex;
  55. };
  56.  
  57. void * cam_read( void * theInfo )
  58. {
  59. int width, height, depth;
  60. int i, result;
  61. unsigned char * image;
  62. struct thread_data *data;
  63. data = (struct thread_data *)theInfo;
  64.  
  65. char prefix[200];
  66. int rc;
  67.  
  68. fflush(stdout);
  69. printf( "%s\n", data->deviceName );
  70. /* rc = pthread_mutex_lock( &a_mutex ); */
  71. /* if( !rc ) */
  72. /* { */
  73. /* printf( "Mutex locked in cam_open\n" ); */
  74. /* } */
  75. /* else */
  76. /* { */
  77. /* printf( "Cant lock mutex in cam_open\n" ); */
  78. /* } */
  79. //data->device = open( data->deviceName, O_RDWR);
  80. data->device = open_camera( data->deviceName, a_mutex );
  81. /* if( !rc ) */
  82. /* { */
  83. /* printf( "Mutex unlocked in cam_open\n" ); */
  84. /* } */
  85. fflush(stdout);
  86. printf( "%s %d\n", "hi", data->device );
  87.  
  88.  
  89. for( i = 0; i < data->maxFrames; i++ )
  90. {
  91. printf( "%s %i\n", "READ FRAME:", i );
  92. // read the cam image
  93. result = qcam_read( data->device, &width, &height, &depth,
  94. &image, a_mutex );
  95. printf( "%d\n", result );
  96. if( !result )
  97. {
  98. printf( "%s %i\n", "BLEAH:", i );
  99. get_actual_image( image, width, height, depth,
  100. data->xOffset, data->yOffset, data->isbottom, 1 );
  101. }
  102.  
  103. if( !data->nopng )
  104. {
  105. if( !data->experiment )
  106. {
  107. sprintf( prefix, "../var/%s_%d/%s_%d %03d.png", data->name, data->cam, data->name, data->cam, i );
  108. }
  109. else if ( data->experiment )
  110. {
  111. sprintf( prefix, "../var/experiments/image_sequences/%s/%s_%d/%s_%d %03d.png", data->name,
  112. data->name, data->cam, data->name,
  113. data->cam, i );
  114. }
  115. // Ends up looknig like "dirname_cam/frame.png"
  116. png_write_qcam( prefix, width, height, i, image, data->isbottom );
  117. //fprintf( stdout, "%s\n", prefix );
  118. }
  119. free( image );
  120. }
  121.  
  122. close_camera( data->device );
  123. pthread_exit(0);
  124. }
  125.  
  126.  
  127.  
  128. int main (int argc, char ** argv)
  129. {
  130. charp image[MAXVIEWS];
  131. int width[MAXVIEWS], height[MAXVIEWS], depth[MAXVIEWS];
  132. int xOffSet[MAXVIEWS], yOffSet[MAXVIEWS];
  133. int error;
  134. int i, j;
  135. int gzpx[MAXVIEWS], gzpy[MAXVIEWS], gzpz[MAXVIEWS];
  136. double pitch[MAXVIEWS], roll[MAXVIEWS], yaw[MAXVIEWS];
  137. int isbottom[MAXVIEWS];
  138. int devices[MAXVIEWS]; // Store the camera device(s)
  139. FILE *stereo; // stereo.dat file pointer
  140. FILE *directory[MAXVIEWS]; // File pointer for the flatview info in each dir
  141. int numDirs = 0; // Holds the number of directorys we will be looking at, must be less than MAXVIEWS.
  142. int maxFrames, currentFrame;
  143. int viewNum; // Stores the current view being looked at.
  144. char name[125] = {};// Stores the name of the capture we are working on
  145. char filename[125];
  146. FILE *fp;
  147. char prefix[125];
  148.  
  149. int nopng = 0; // if 1 no png will be writen.
  150. int up_down = 0;
  151. int ud_cam = 0;
  152. int go = 0;
  153. int experiment = 0; // if 1 this is capturing an experiment and will create the corect directories accordingly.
  154.  
  155. pthread_t threads[MAXVIEWS] = {};
  156. struct thread_data *info[MAXVIEWS] = {};
  157.  
  158.  
  159. // Get the program flags
  160. for( i = 0; i < argc; i++ )
  161. {
  162. if( !strcmp( argv[i], "-npng" ))
  163. nopng = 1;
  164. if( !strcmp( argv[i], "-name" ))
  165. strcpy( name, argv[++i] );
  166. if( !strcmp( argv[i], "-exp" ))
  167. experiment = 1;
  168. if( !strcmp( argv[i], "-cams" ))
  169. numDirs = argv[++i];
  170. if( !strcmp( argv[i], "-u" ))
  171. up_down = 1;
  172. if( !strcmp( argv[i], "-go" ))
  173. go = 1;
  174. }
  175.  
  176. if( !go )
  177. {
  178. // First get all the info from the user about what we will be caputring
  179. if( !strcmp( name, "" ) && !nopng)
  180. {
  181. fprintf( stdout, "What will this test be called? " );
  182. fscanf( stdin, "%s", name );
  183. }
  184.  
  185. if( numDirs == 0 )
  186. {
  187. fprintf( stdout, "How many cameras will we be capturing from? " );
  188. fscanf( stdin, "%d", &numDirs );
  189. }
  190.  
  191. if( up_down )
  192. {
  193. fprintf( stdout, "What camera is upside down? " );
  194. fscanf( stdin, "%d", &ud_cam );
  195. }
  196. }
  197. for( i = 0; i < numDirs; i++ )
  198. {
  199. if( !nopng && !go )
  200. {
  201. // Get the x, y, z, yaw, pitch, roll for each camera
  202. fprintf( stdout, "cam %d X pos: ", i+1 );
  203. fscanf( stdin, "%d", &gzpx[i] );
  204. fprintf( stdout, "cam %d Y pos: ", i+1 );
  205. fscanf( stdin, "%d", &gzpy[i] );
  206. fprintf( stdout, "cam %d Z pos: ", i+1 );
  207. fscanf( stdin, "%d", &gzpz[i] );
  208.  
  209. fprintf( stdout, "cam %d Yaw: ", i+1 );
  210. fscanf( stdin, "%lf", &yaw[i] );
  211. fprintf( stdout, "cam %d Pitch: ", i+1 );
  212. fscanf( stdin, "%lf", &pitch[i] );
  213. fprintf( stdout, "cam %d Roll: ", i+1 );
  214. fscanf( stdin, "%lf", &roll[i] );
  215.  
  216. fprintf( stdout, "If the camera is upside down enter 1, otherwise enter 0. " );
  217. fscanf( stdin, "%d", &isbottom[i] );
  218.  
  219. // Make the directories that the pics will be stored in
  220. {
  221. if( !experiment )
  222. {
  223. sprintf( filename, "mkdir ../var/%s_%d", name, i );
  224. //printf( "%s\n", filename );
  225. }
  226. else if ( experiment )
  227. {
  228. sprintf( filename, "mkdir ../var/experiments/image_sequences/%s/%s_%d", name, name, i );
  229. }
  230. system( filename );
  231. }
  232. }
  233. else
  234. {
  235. gzpx[i] = 0;
  236. gzpy[i] = 0;
  237. gzpz[i] = 0;
  238. yaw[i] = 0;
  239. pitch[i] = 0;
  240. roll[i] = 0;
  241. if( up_down == 1 && (ud_cam - 1) == i )
  242. isbottom[i] = 1;
  243. else
  244. isbottom[i] = 0;
  245. }
  246. }
  247.  
  248. if( !nopng )
  249. {
  250. fprintf( stdout, "How many frames will we capture? " );
  251. fscanf( stdin, "%d", &maxFrames );
  252. }
  253. else
  254. {
  255. maxFrames = 5000;
  256. }
  257.  
  258.  
  259. // init the display
  260. init_display(argv[0],WINDOWWIDTH,WINDOWHEIGHT);
  261.  
  262.  
  263. // Setup the x and y offset for the images
  264. for( i = 0; i < numDirs; i++ )
  265. {
  266. if( i < 2 )
  267. {
  268. xOffSet[i] = ( i % 3 ) * 320;
  269. yOffSet[i] = ( i / 2 ) * 240;
  270. }
  271. if( i >= 2 )
  272. {
  273. xOffSet[i] = (( i + 1 ) % 3 ) * 320;
  274. yOffSet[i] = (( i + 1 ) / 2 ) + 240;
  275. }
  276. }
  277.  
  278. // Write the .dat file for each directory
  279. if( !nopng )
  280. {
  281. for( i = 0; i < numDirs; i++ )
  282. {
  283. if( !experiment )
  284. {
  285. sprintf( filename, "../var/%s_%d.dat", name, i );
  286. sprintf( prefix, "%s_%d/%s_%d", name, i, name, i );
  287. }
  288. else if ( experiment )
  289. {
  290. sprintf( filename, "../var/experiments/image_sequences/%s/%s_%d.dat", name, name, i );
  291. sprintf( prefix, "experiments/image_sequences/%s/%s_%d/%s_%d", name, name, i, name, i );
  292. }
  293. fp = fopen( filename, "w" );
  294. fprintf( fp, "%s\n%d\n%d\n%d\n%d %d %d\n%lf %lf %lf\n", prefix, 0,
  295. currentFrame, 3, gzpx[i], gzpy[i], gzpz[i],
  296. yaw[i], pitch[i], roll[i] );
  297. fclose( fp );
  298. }
  299. }
  300.  
  301.  
  302. // Make all the threads
  303. for( i = 0; i < numDirs; i++ )
  304. {
  305. info[i] = ( struct thread_data* )malloc( sizeof( struct thread_data ));
  306. sprintf( info[i]->deviceName, "/dev/video%d", i );
  307. strcpy( info[i]->name, name );
  308. info[i]->maxFrames = maxFrames;
  309. info[i]->xOffset = xOffSet[i];
  310. info[i]->yOffset = yOffSet[i];
  311. info[i]->nopng = nopng;
  312. info[i]->experiment = experiment;
  313. info[i]->cam = i;
  314. info[i]->isbottom = isbottom[i];
  315. //info[i]->the_mutex = a_mutex;
  316.  
  317. pthread_create( &threads[i], 0,
  318. cam_read,
  319. (void *)info[i]);
  320. }
  321.  
  322. /* for( i = 0; i < numDirs; i++ ) */
  323. /* { */
  324. /* pthread_join(info[i], 0); */
  325. /* } */
  326.  
  327.  
  328. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 436
Reputation: Chainsaw is an unknown quantity at this point 
Solved Threads: 10
Chainsaw's Avatar
Chainsaw Chainsaw is offline Offline
Unprevaricator

Re: Multithreading problem

 
0
  #2
Aug 27th, 2004
Why does the camera open need the mutex? Maybe it doesn't release the mutex so the code never gets in a second time?

Maybe the mutex starts out busy so no one can get it?
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 18
Reputation: hivework is an unknown quantity at this point 
Solved Threads: 0
hivework hivework is offline Offline
Newbie Poster

Re: Multithreading problem

 
0
  #3
Aug 27th, 2004
try running two single threaded programs at the same time. see if two different cameras can be accessed at the same time.
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 18
Reputation: hivework is an unknown quantity at this point 
Solved Threads: 0
hivework hivework is offline Offline
Newbie Poster

Re: Multithreading problem

 
0
  #4
Aug 27th, 2004
Originally Posted by Chainsaw
Why does the camera open need the mutex? Maybe it doesn't release the mutex so the code never gets in a second time?

Maybe the mutex starts out busy so no one can get it?
i agree. maybe pthread_mutex_unlock function will help?
Reply With Quote Quick reply to this message  
Join Date: Aug 2004
Posts: 5
Reputation: ArmedCoder is an unknown quantity at this point 
Solved Threads: 0
ArmedCoder ArmedCoder is offline Offline
Newbie Poster

Re: Multithreading problem

 
0
  #5
Aug 27th, 2004
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 436
Reputation: Chainsaw is an unknown quantity at this point 
Solved Threads: 10
Chainsaw's Avatar
Chainsaw Chainsaw is offline Offline
Unprevaricator

Re: Multithreading problem

 
0
  #6
Aug 27th, 2004
I don't see a call to pthread_mutex_init(). Did I just miss it? You need to call that on the mutex before you start, don't you?
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 18
Reputation: hivework is an unknown quantity at this point 
Solved Threads: 0
hivework hivework is offline Offline
Newbie Poster

Re: Multithreading problem

 
0
  #7
Aug 27th, 2004
What's in the "ifqcam.h" file? Did you write the qcam_ functions? If so, maybe the problem is in that code.
Reply With Quote Quick reply to this message  
Join Date: Aug 2004
Posts: 5
Reputation: ArmedCoder is an unknown quantity at this point 
Solved Threads: 0
ArmedCoder ArmedCoder is offline Offline
Newbie Poster

Re: Multithreading problem

 
0
  #8
Aug 27th, 2004
"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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2004
Posts: 5
Reputation: ArmedCoder is an unknown quantity at this point 
Solved Threads: 0
ArmedCoder ArmedCoder is offline Offline
Newbie Poster

Re: Multithreading problem

 
0
  #9
Aug 27th, 2004
Originally Posted by hivework
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.

  1. int open_camera(const char *devicename, pthread_mutex_t mutex)
  2. {
  3. int rc;
  4. rc = pthread_mutex_lock( &a_mutex );
  5. if( !rc )
  6. {
  7. printf( "Mutex locked in cam_open\n" );
  8. }
  9. else
  10. {
  11. printf( "Cant lock mutex in cam_open\n" );
  12. }
  13. int device = open(devicename, O_RDWR);
  14. pthread_mutex_unlock( &a_mutex );
  15. if( !rc )
  16. {
  17. printf( "Mutex unlocked in cam_open\n" );
  18. }
  19. if(device <= 0)
  20. {
  21. printf("Device %s couldn't be opened\n", devicename);
  22. return 0;
  23. }
  24. //return 1;
  25. return device;
  26. }

and the qcam_read function the thread never even makes it to at all.
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 18
Reputation: hivework is an unknown quantity at this point 
Solved Threads: 0
hivework hivework is offline Offline
Newbie Poster

Re: Multithreading problem

 
0
  #10
Aug 27th, 2004
Originally Posted by ArmedCoder
the only code in that file im using is the open_camera() and the qcam_read() functions.

  1. int open_camera(const char *devicename, pthread_mutex_t mutex)
  2. {
  3. int rc;
  4. rc = pthread_mutex_lock( &a_mutex );
  5. if( !rc )
  6. {
  7. printf( "Mutex locked in cam_open\n" );
  8. }
  9. else
  10. {
  11. printf( "Cant lock mutex in cam_open\n" );
  12. }
  13. int device = open(devicename, O_RDWR);
  14. pthread_mutex_unlock( &a_mutex );
  15. if( !rc )
  16. {
  17. printf( "Mutex unlocked in cam_open\n" );
  18. }
  19. if(device <= 0)
  20. {
  21. printf("Device %s couldn't be opened\n", devicename);
  22. return 0;
  23. }
  24. //return 1;
  25. return device;
  26. }

and the qcam_read function the thread never even makes it to at all.
The problem could be that you release your mutex too soon. The program is calling pthread_mutex_unlock() right after open(). Try releasing the mutex after the close() function.

Can you show us your qcam_read() code?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC