Jump to content
  • Advertisement
Sign in to follow this  
Palidine

Thread safe variable read

This topic is 4029 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
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.)

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!