CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    May 2005
    Posts
    112

    thread safe data

    hello,

    In C, if threadA has the ability to read and write to a complex data object and threadB is guaranteed to be read only, does the data object have to be thread safe?

    since a writer exists, are you subject to race condition between threads?
    But is this the only side effect, or is there the potential for memory volatility/corruption?

    thanks.

  2. #2
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: thread safe data

    Quote Originally Posted by Moore View Post
    hello,

    In C, if threadA has the ability to read and write to a complex data object and threadB is guaranteed to be read only, does the data object have to be thread safe?

    since a writer exists, are you subject to race condition between threads?
    But is this the only side effect, or is there the potential for memory volatility/corruption?

    thanks.
    You risk reading a corrupt object.

    Image your "complex data" holds a char array, and an int representing the size.

    Method write() changes the array, and THEN changes the int size.
    Imagine if method read() were to read said object between those two actions.

    Any time you have threads, you cannot read/write an object at the same time. I'm not even sure about simply atomic data like "int".

  3. #3
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: thread safe data

    Nope, not even primitives.

    Having multiple readers at the same time is fine, though, unless something about the operation of the underlying data structure says otherwise. (Multiple readers on STL containers are safe.)

  4. #4
    Join Date
    May 2005
    Posts
    112

    Re: thread safe data

    thanks for feedback

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured