Sign in to follow this  
Palidine

Thread safe variable read

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.)

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this