473,769 Members | 5,784 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Multithreading problem


Hi, im just learning about multithreading for a program i am writin
that needs to read from multiple camreas attached to the computer a
the same time.
I know how to create threads and pass around info, and i know a bi
about mutexes but this is not where the problem resides.
Every time the thread goes to open the device at /dev/video* the threa
just stops running as far as i can see.
i put printf's all over the place and it prints right before the ope
call on the camera, then the next thread kicks in and does the sam
thing.
I know the camreas work cuse when i go back to the non-multithreade
code, everything works just fine.
Also if i try the open call outside the thread, then pass the camre
into the thread, they open fine but when the read() call comes, the
the thread dies in the same way.
Does anyone have any ideas whats going on?
Here is the code for the program.

Code
-------------------
#include "dispimg.h"
#include "imggroup.h "
#include "ifpng.h"
#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 all files can see
int imgwidth = SETWIDTH;
int imgheight = SETHEIGHT;
int imgdepth = SETDEPTH;
extern Display *display;
extern Window win;

typedef unsigned char * charp;

pthread_mutex_t a_mutex = PTHREAD_MUTEX_I NITIALIZER;
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_l ock( &a_mutex ); */
/* if( !rc ) */
/* { */
/* printf( "Mutex locked in cam_open\n" ); */
/* } */
/* else */
/* { */
/* printf( "Cant lock mutex in cam_open\n" ); */
/* } */
//data->device = open( data->deviceName, O_RDWR);
data->device = open_camera( data->deviceName, a_mutex );
/* if( !rc ) */
/* { */
/* printf( "Mutex unlocked in cam_open\n" ); */
/* } */
fflush(stdout);
printf( "%s %d\n", "hi", 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_imag e( 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_ca m/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) ;
}

int main (int argc, char ** argv)
{
charp image[MAXVIEWS];
int width[MAXVIEWS], height[MAXVIEWS], depth[MAXVIEWS];
int xOffSet[MAXVIEWS], yOffSet[MAXVIEWS];
int error;
int i, j;
int gzpx[MAXVIEWS], gzpy[MAXVIEWS], gzpz[MAXVIEWS];
double pitch[MAXVIEWS], roll[MAXVIEWS], yaw[MAXVIEWS];
int isbottom[MAXVIEWS];
int devices[MAXVIEWS]; // Store the camera device(s)
FILE *stereo; // stereo.dat file pointer
FILE *directory[MAXVIEWS]; // File pointer for the flatview info in each dir
int numDirs = 0; // Holds the number of directorys we will be looking at, must be less than MAXVIEWS.
int maxFrames, currentFrame;
int viewNum; // Stores the current view being looked at.
char name[125] = {};// Stores the name of the capture we are working on
char filename[125];
FILE *fp;
char prefix[125];

int nopng = 0; // if 1 no png will be writen.
int up_down = 0;
int ud_cam = 0;
int go = 0;
int experiment = 0; // if 1 this is capturing an experiment and will create the corect directories accordingly.

pthread_t threads[MAXVIEWS] = {};
struct thread_data *info[MAXVIEWS] = {};
// Get the program flags
for( i = 0; i < argc; i++ )
{
if( !strcmp( argv[i], "-npng" ))
nopng = 1;
if( !strcmp( argv[i], "-name" ))
strcpy( name, argv[++i] );
if( !strcmp( argv[i], "-exp" ))
experiment = 1;
if( !strcmp( argv[i], "-cams" ))
numDirs = argv[++i];
if( !strcmp( argv[i], "-u" ))
up_down = 1;
if( !strcmp( argv[i], "-go" ))
go = 1;
}

if( !go )
{
// First get all the info from the user about what we will be caputring
if( !strcmp( name, "" ) && !nopng)
{
fprintf( stdout, "What will this test be called? " );
fscanf( stdin, "%s", name );
}

if( numDirs == 0 )
{
fprintf( stdout, "How many cameras will we be capturing from? " );
fscanf( stdin, "%d", &numDirs );
}

if( up_down )
{
fprintf( stdout, "What camera is upside down? " );
fscanf( stdin, "%d", &ud_cam );
}
}
for( i = 0; i < numDirs; i++ )
{
if( !nopng && !go )
{
// Get the x, y, z, yaw, pitch, roll for each camera
fprintf( stdout, "cam %d X pos: ", i+1 );
fscanf( stdin, "%d", &gzpx[i] );
fprintf( stdout, "cam %d Y pos: ", i+1 );
fscanf( stdin, "%d", &gzpy[i] );
fprintf( stdout, "cam %d Z pos: ", i+1 );
fscanf( stdin, "%d", &gzpz[i] );

fprintf( stdout, "cam %d Yaw: ", i+1 );
fscanf( stdin, "%lf", &yaw[i] );
fprintf( stdout, "cam %d Pitch: ", i+1 );
fscanf( stdin, "%lf", &pitch[i] );
fprintf( stdout, "cam %d Roll: ", i+1 );
fscanf( stdin, "%lf", &roll[i] );

fprintf( stdout, "If the camera is upside down enter 1, otherwise enter 0. " );
fscanf( stdin, "%d", &isbottom[i] );

// Make the directories that the pics will be stored in
{
if( !experiment )
{
sprintf( filename, "mkdir ../var/%s_%d", name, i );
//printf( "%s\n", filename );
}
else if ( experiment )
{
sprintf( filename, "mkdir ../var/experiments/image_sequences/%s/%s_%d", name, name, i );
}
system( filename );
}
}
else
{
gzpx[i] = 0;
gzpy[i] = 0;
gzpz[i] = 0;
yaw[i] = 0;
pitch[i] = 0;
roll[i] = 0;
if( up_down == 1 && (ud_cam - 1) == i )
isbottom[i] = 1;
else
isbottom[i] = 0;
}
}

if( !nopng )
{
fprintf( stdout, "How many frames will we capture? " );
fscanf( stdin, "%d", &maxFrames );
}
else
{
maxFrames = 5000;
}
// init the display
init_display(ar gv[0],WINDOWWIDTH,WI NDOWHEIGHT);
// Setup the x and y offset for the images
for( i = 0; i < numDirs; i++ )
{
if( i < 2 )
{
xOffSet[i] = ( i % 3 ) * 320;
yOffSet[i] = ( i / 2 ) * 240;
}
if( i >= 2 )
{
xOffSet[i] = (( i + 1 ) % 3 ) * 320;
yOffSet[i] = (( i + 1 ) / 2 ) + 240;
}
}

// Write the .dat file for each directory
if( !nopng )
{
for( i = 0; i < numDirs; i++ )
{
if( !experiment )
{
sprintf( filename, "../var/%s_%d.dat", name, i );
sprintf( prefix, "%s_%d/%s_%d", name, i, name, i );
}
else if ( experiment )
{
sprintf( filename, "../var/experiments/image_sequences/%s/%s_%d.dat", name, name, i );
sprintf( prefix, "experiment s/image_sequences/%s/%s_%d/%s_%d", name, name, i, name, i );
}
fp = fopen( filename, "w" );
fprintf( fp, "%s\n%d\n%d\n%d \n%d %d %d\n%lf %lf %lf\n", prefix, 0,
currentFrame, 3, gzpx[i], gzpy[i], gzpz[i],
yaw[i], pitch[i], roll[i] );
fclose( fp );
}
}
// Make all the threads
for( i = 0; i < numDirs; i++ )
{
info[i] = ( struct thread_data* )malloc( sizeof( struct thread_data ));
sprintf( info[i]->deviceName, "/dev/video%d", i );
strcpy( info[i]->name, name );
info[i]->maxFrames = maxFrames;
info[i]->xOffset = xOffSet[i];
info[i]->yOffset = yOffSet[i];
info[i]->nopng = nopng;
info[i]->experiment = experiment;
info[i]->cam = i;
info[i]->isbottom = isbottom[i];
//info[i]->the_mutex = a_mutex;

pthread_create( &threads[i], 0,
cam_read,
(void *)info[i]);
}

/* for( i = 0; i < numDirs; i++ ) */
/* { */
/* pthread_join(in fo[i], 0); */
/* } */
}
--------------------


--
ArmedCoder
------------------------------------------------------------------------
Posted via http://www.codecomments.com
------------------------------------------------------------------------

Nov 14 '05 #1
2 4092
In article <Ar************ ***@mail.codeco mments.com>,
Ar************* **@mail.codecom ments.com says...

Hi, im just learning about multithreading for a program i am writing


[snip]

c.l.c doesn't discuss programs that use extension not in the ANSI/ISO
C standards. pthreads are a POSIX standard, and unfortunately for you,
Off-topic here. comp.programmin g.threads would be a good place to try
instead.

--
Randy Howard
To reply, remove FOOBAR.
Nov 14 '05 #2
ArmedCoder <Ar************ ***@mail.codeco mments.com> writes:
Hi, im just learning about multithreading for a program i am writing
that needs to read from multiple camreas attached to the computer at
the same time.


Sorry, we don't do multithreading here. It's not supported in
standard C, only in extensions to the language.

Try a newsgroup that talks about either threading or your operating
system. comp.programmin g.threads looks promising, but you might want
to lurk there before posting (I know nothing about it other than its
name).

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
4269
by: Mark Yudkin | last post by:
The documentation is unclear (at least to me) on the permissibility of accessing DB2 (8.1.5) concurrently on and from Windows 2000 / XP / 2003, with separate transactions scope, from separate threads of a multithreaded program using embedded SQL. Since the threads do not need to share transaction scopes, the sqleAttachToCtx family of APIs do not seem to be necessary. <quote> In the default implementation of threaded applications against...
16
8510
by: Robert Zurer | last post by:
Can anyone suggest the best book or part of a book on this subject. I'm looking for an in-depth treatment with examples in C# TIA Robert Zurer robert@zurer.com
6
2316
by: Michael C | last post by:
Hello Can someone please tell me what I'm doing wrong? I'm writing an application that should be using callbacks to perform asynchronous calls to the Win32 API. Problem is it never reaches my callback function. Any feedback is appreciated. Also, any references to websites with examples of .NET async multithreading would be appreciated. Thanks in advance. public serverInfo getSession(string servername) {
5
2139
by: sarge | last post by:
I would like to know how to perform simple multithreading. I had created a simple form to test out if I was multithreading properly, but got buggy results. Sometime the whole thig would lock up when I got two threads going at the same time. What I have is two text boxes (textBox1 and textBox2) and four buttons(cmdStartThread1, cmdStartThread2, cmdStopThread1, cmdStopThread2)
2
3741
by: shonend | last post by:
**** sorry about the length of the message. If you can't read the whole thing and still willing to help, read the last 2 paragraphs where the main problem is described. The introduction story is mentioned to, as much clear as possible, give a picture in what environment the problems rise**** Hello experts! I would appreciate if you can address this problem I have and give a hint what could be wrong.
2
3845
by: Multithreading problem in vb.net | last post by:
Greetings, I am new to multithreading and I am trying to implement it in my app. This application is distributed application which needs to refresh every say 5 secs to show some activities in the datagrid. I have implemented querying the database in a separate thread and and then showing it in the datagrid in the UI thread. It all works fine and the datagrid gets updated every 5 secs. This happens in the desktop (Main form) of the...
4
1572
by: Michael | last post by:
Hi, I am trying to create a multithreaded VB 2005 application which attempts to create a new thread per Domain Controller (DC) in my environment. Each thread connects to its allocated DC and enumerates all computer objects and extracts the 'LastLogon' property. The results from each thread is then consolidated so that I can get the true lastlogon date for each computer object. However in my routine thats get actioned per thread, I have...
0
1261
by: denis.cornehl | last post by:
Hi, I have an unusual Problem with DB2. It is DB2 Version 7 and Fixpack 13 under Windows. We have written an application server which is accessing db2 via c++ and the cli interface. We used IBM Visual Age as our compiler, because we had to support OS/2. Now we ported it this year to windows and the microsoft-compiler (V8).
2
2267
by: Pradnya Patil | last post by:
hi , I am trying to draw ' html div-tag ' on the screen which will resemble a rectangle through vb.net code. I want it to be drawn faster...so I introduced multithreading using Threadpool. I divided the complete drawing into 3 parts..1st will be done by main thread and other two are done in these procedures - <1LongTimeTask <2LongTimeTask2 I have invoked the threads using below method. **************
7
16312
by: Ray | last post by:
Hello, Greetings! I'm looking for a solid C++ multithreading book. Can you recommend one? I don't think I've seen a multithreading C++ book that everybody thinks is good (like Effective C++ or Exceptional C++, for example). Platform-specific (e.g.: Win32, POSIX) is OK, as long as it's good :) Thank you, Ray
0
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10216
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9865
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8873
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5310
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3965
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.