volatile, shared memory, and synchronisation

Chris Torek chris at mimsy.UUCP
Tue May 24 06:04:04 AEST 1988


In article <199 at gannet.cl.cam.ac.uk> scc at cl.cam.ac.uk (Stephen Crawley) writes:
>... multi-process applications that use shared memory.  Consider two
>processes A and B with a shared variable v, and the following code
>   int x = v	/* A1 */	|	v = 2	/* B1 */
>   int y = v	/* A2 */	|	

Even *with* volatile declarations, you get no solid guarantee.  Suppose
that v is a `volatile int', and that you are running on a bit-oriented
binary machine and v has been placed across two different memory
banks.  The sequence might be A1, B1 <first half>, A2, B1 <second
half>:

A1:	x = 1			| v = 0...0 1
B1:	v = 2	<first half>	| v = 0...0 0 <lower write complete>
A2:	y = 0			| v = 0...0 0 <upper write incomplete>
				| v = 0...1 0 <upper write complete>

This is perhaps unlikely, but in fact something similar could
easily happen on a multi-cpu R2000-based system (assuming 32 bit
integers), although not with the values 1 and 2.

Volatility is only part of what you need for synchronisation.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris at mimsy.umd.edu	Path:	uunet!mimsy!chris



More information about the Comp.lang.c mailing list