develooper Front page | perl.perl5.porters | Postings from August 2001

Thread memory visibility

Thread Next
From:
Arthur Bergman
Date:
August 22, 2001 12:07
Subject:
Thread memory visibility
Message ID:
007f01c12b3e$bfcc79a0$052aa8c0@foo
This is quoted from Programming with POSIX threads by David R. Butenhof

Following are the rules for memory visbility to be followed by any pthread implmentation.

1 Whatever memory values a thread can see when it calls pthread_create can also be seen by the new thread when it starts. Any data written to memory after the call to pthread_create may not necessarily be seen by the new thread, even if the write occurs before the thread starts.

2. Whatever memory values a thread can see when it unlocks a mutex, either directly or waiting on a condition variable, can also been seen by any thread that later locks the same mutex. Again, data written after the mutex is unlocked may not necessarily be seen by the thread that locks the mutex, even if the write occurs before the lock.

3. Whatever memory values a thread can see when it terminates, either by cancellation, returning from the start function, or by calling pthread_exit, can also been seen bu the thread that joins with the terminated thread by calling pthread_join. And ofcourse, data written after the thread terminates may not necessarily be seen by the thread that joins, even if the write occurs before the join.

4. Whatever memory values a thread can see when it signals or broadcasts a condition variable can also be seen by any thread that is awakened by the signal or broadcast. And, one more time, data written after the signal or broadcast may not necessarily be seen by the thread that wakes up, even if the write occurs before it awakens.

----

This is the standard, Dans comment that a change that dribbled out into memory is globally visible is usually true, (but just as Dan says, it isn't always). On single CPU machines these rules are usually not relevant. On a multi CPU they usually are, on big multi CPU they defintly are ;)

They are good to remeber.

Arthur


Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About