Thread safe variable read

Started by
2 comments, last by Raghar 16 years, 9 months ago
Say I have a container operating in an SMP environment:

class FIFOQueue
{
private:
    Data *head;
};

Do I need a lock to read the value of head? I know i'd need a lock or a CAS to write it. Is there an equivalent to CAS for reading via which to avoid the use of locks? -me
Advertisement
Assuming your target platform has pointers that are only one word in size (which is generally the case, but not always) and assuming the pointer is stored with proper alignment (also generally the case), then reading the value of head should be safe. (For a given value of "safe". The pointer itself should always be a value that some thread has stored to head. Whether if reading it's data members is safe or even if it points to valid data is another story.)
Quote:Original post by SiCrane
Assuming your target platform has pointers that are only one word in size (which is generally the case, but not always) and assuming the pointer is stored with proper alignment (also generally the case), then reading the value of head should be safe. (For a given value of "safe". The pointer itself should always be a value that some thread has stored to head. Whether if reading it's data members is safe or even if it points to valid data is another story.)


Yeah ok. Good. All the other issues I think I've got a handle on for now.

-me

Quote:Original post by SiCrane
Assuming your target platform has pointers that are only one word in size

Actually max size of atomic register to memory operant. A word is 16 bit number in assembly. ~_^


Re OP

I'd look at IBM
c2 wiki
wait free sync
And create few test programs, just to get some right feeling with concepts. I often do these things intuitively.

Actually I think you might have problems with reading and writing into it because it's private variable. BTW as long as it's in one thread, you can do with it anything you want.

This topic is closed to new replies.

Advertisement