To mutex or not to mutex an int global variable ??
Frank, Nice *idea*, but no marbles. 🙁 Lots of variations of this idea are proposed regularly, starting with Dekker’s algorithm and going on and on. The problem is that you are assuming that writes from the CPU are arriving in main memory in order. They may not. On all high performance CPUs today (SPARC, Alpha, PA-RISC, MIPS, x86, etc.) out-of-order writes are allowed. Hence in your example below, it is POSSIBLE that only one of the values will be updated before the pointer is swapped. Bummer, eh? -Bil “Use the mutex, Luke!” > To avoid locks you might try the following trick: > Replace the two ints by a struct that contains total and fraction. The global > variable would then be a pointer to the struct. To modify the variables, the writer > would use a temporary struct, update the value(s) and then swap the pointers of the > global pointer and the temporary global pointer. This works assuming that a pointer > write is an atomic operation (which is the case in all architectures I know)