Hi all,
It's probably not the right mailing list to ask but I am really
surprised about global variable sharing in a multithreaded C
application. If I remember well my multithreading course global
variables are shared between threads, right ?
 
Example :
----------------------------
int counter = 0;
int main() {
  if( fork()==0) {
    while(1) {
      sleep(1);
      counter++;
      printf("Son : counter = %d\n", counter);      
    }    
  } else {
    while(1) {
      sleep(1);
      printf("Parent : counter = %d\n", counter);      
    }
 }       
  return 0;
}
----------------------------

All I get is :
Parent : counter = 0
Son : counter = 1
Son : counter = 2
Parent : counter = 0
Son : counter = 3
Parent : counter = 0
Son : counter = 4
Parent : counter = 0

why counter isn't shared between the two threads ??!
thanks,
-aziz
_______________________________________________
freebsd-hackers@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
To unsubscribe, send any mail to "[EMAIL PROTECTED]"

Reply via email to