Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

strange pthreads behaviour

Status
Not open for further replies.

Stovio

Programmer
Mar 20, 2002
30
SE
Hi,

Let me know if this is the wrong forum but i couldn't find any forum here for threads so i give it a go.

I am using pthreads and c with VS6 to make a threaded program on my p4 with MS Windows XP Pro. I started out with a basic program that is supposed to start one "car" thread and five "passanger" threads. Each of the threads print a fiew words to show that they are running. The weird thing is that sometimes more prints are being made than there are threads, some threads seem to print twice. There is no loop in the function bodies. Also sometimes some letters are missing in the words or some letter is written more than once and sometimes the program hangs. This does not happen very often. Anybody know why?

Here is the code:


#ifndef _REENTRANT
#define _REENTRANT
#endif

#define N 5
#define C 3

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>

void *carFunc(void *inCol);
void *pasFunc(void *inCol);

int main(int argc, char* argv[])
{
pthread_t car;
pthread_t passenger[N];

pthread_create(&car, NULL, carFunc, NULL);
for(int i = 0; i < N; i++)
pthread_create(&passenger, NULL, pasFunc, NULL);

pthread_join(car, NULL);
for(i = 0; i < N; i++)
pthread_join(passenger, NULL);

return(0);
}

void *carFunc(void *inCol){

printf("In carFunc\n");

return (NULL);
}

void *pasFunc(void *inCol){

printf("In pasFunc\n");

return (NULL);
}

Thanks for any help!

/Henrik
 
I don't know much about pthreads -- hell, I didn't even know you could run pthreads on Windows! But I'm wondering if you might need some locks when the threads are printing. Maybe only part of the printf() is getting processed, then it switches to another thread?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top