Subj : Re: deadlock avoidance To : comp.programming.threads From : Joe Seigh Date : Thu Jul 07 2005 08:29 am Satendra wrote: > Hi Jose, > I guess my statement was misinterpreted. What i meant by atomic here > is that if we have lets say an integer i with initial value 0 .Thread > T1 unconditionally modified to 2 (i = 2) then T2 at any time will read > it as either 0 or 2 but nothing in between, I agree that with c++ > objects this might not be true. I still don't understand why a function > who is just checking/reading the value of a global variable can't be > reentrant ? > If the variable is properly initialized before the thread is started or you have some other mechanism to ensure that it is correctly initialized (pthread_once or something) it's ok to read it without a lock. If you're going to change the value of the variable in another thread, then you need to be a little careful. Depending on how you are going to use it you may need atomicity which is not addressed by Posix pthreads unless you use a mutex. If you don't use a mutex, you'll have to provide your own mechanism for atomic access and you'll have to address portability issues on your own. If the global variable is only changed once then it is safe to read it even without atomic access provide your logic can handle possible multiple undefined values during the update of the variable, e.g. static int x = 0; while (x != 0) { do something... } works even if some of the undefined values are zero during the update of x going from 0 to some other value. And mutexes have nothing to do with forward progress so using one wouldn't necessarily guarantee forward progress better than not using a mutex. In fact in all of the implementations I'm aware of mutexes slow things down. -- Joe Seigh When you get lemons, you make lemonade. When you get hardware, you make software. .