When using multiple threads in userspace together with global data structures
(bss, data segment or heap)  the following is not clear to me:
Guess only one thread fills and updates a more complex global data structure 
(similar to a devices softstate if it was in the kernel) and multiple other
threads shall access the data from this structure. 
Do I really need a mutex, rw-lock or something of that kind around every single
access - be it read or write - to any member of this structure?
Or would it be enough to declare the respective structure/members volatile.
What makes me unsure about correct usage is the statement from the 
"Multithreaded
Programming Guide":
"The synchronization primitives use special instructions that flush the store 
buffers
to cache...So, using locks around your shared data ensures memory consistency."
On the other hand "Writing Device Drivers" contains an example with a busy flag
not protected by a lock and using the volatile keyword instead.
Several small code examples in "Multithreaded Programming Guide" access data
that are not in the current threads stack without using locks.
So, the case described in the beginning does not require a lock for correct 
data in
the structure since only one thread is doing updates. But when is the data 
visible
to other threads? Or which instruction causes the data to be written to memory
so that a consumer using "volatile" sees the data? The same question would 
occurr
with a global data buffer being shared between several threads.
In practice everything works fine with volatile but the manual says (as far as 
I 
understand it) that there might be problems.
Any hint would be very useful.
-- 
This message posted from opensolaris.org

Reply via email to