New to threads

From: El Durango (El_Durango_at_yah00.c0m)
Date: 03/15/04


Date: Mon, 15 Mar 2004 06:39:47 GMT

Hi I am new to learning about threads.
I have been working on example code and had a question regarding the
following piece of code based on threads:
/**************************************************************/
#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>

#define NLOOP 5000

int counter;

void *doit(void *);

int main(int argc, char **argv){
   pthread_t tidA, tidB;
   char *name = "Steve";

   if(argc == 2)
      name = argv[1];
   pthread_create(&tidA, NULL, &doit, NULL);
   pthread_create(&tidB, NULL, &doit, NULL);
   /* wait for both threads to terminate */
   printf("name: %s\tcounter:%d\n",name,counter);
   pthread_join(tidA, NULL);
   pthread_join(tidB, NULL);

   exit(0);
}

void* doit(void *vptr){
   int i, val;
   for(i=0; i < NLOOP; i++){
      val = counter;
      printf("%d: %d\n",pthread_self(), val+1);
      counter = val+1;
   }
   return (NULL);
}
/**********************************************************/

in this code I do not understand how the function pthread_join works, why I
say this is because if I comment out the pthread_join system calls then the
doit function that the pthread_create calls does not run. I even commented
the 2nd pthread_create sys. call just to see if it changed the issue, but it
did not.
So basically in this example the pthread_create is relian on the
pthread_join sys. call to effectively call and run the function. I am
wondering why this is? and if so when is it not reliant on the pthread_join,
again I even tried with only one thread and it still did not work. From my
reading I believe pthread_join is used when there are multiple threads that
are created, no?
If anyone can shed light my ignorance I would appreciate it.
I know my question is probably silly to most of you but it's better that I
ask then remain ignorant.
thank you again.



Relevant Pages