Subj : Re: 'volatile' Rules To : comp.programming.threads From : Joe Seigh Date : Tue Jun 07 2005 09:38 pm Marcin 'Qrczak' Kowalczyk wrote: > Joe Seigh writes: >>>In practice most programs using signals assume slightly more than >>>that, even though it's not formally guaranteed by ISO C or POSIX. >>>They assume that volatile variables which fit into machine word >>>are accessed atomically wrt. a signal handler. I haven't heard of >>>a platform where this is not true. >> >>They assume too much. Volatile doesn't mean anything here. > > > It ensures that the main program doesn't cache the variable in a > register, so that its change by a signal handler will be seen > immediately when the main program accesses it, and its change by the > main program will be seen immediately if a signal arrives shortly > after. Until the signal handler exits, the main program doesn't see or do anything. After the signal handler exits, the main program sees everything the signal handler saw. I don't know what "immediate" buys you. The signal could occur just before the store instruction even for volatile variables. If you need to coordiate something with a signal handler you should be using the signal handling control functions to do that. > > volatile variables are the only reliable means of communication of > data between a signal handler and the main program. I never depend on volatile for communicating with signal handlers. I use other mechanisms for correct memory visibility. -- Joe Seigh When you get lemons, you make lemonade. When you get hardware, you make software. .