-
April 28th, 2010, 06:28 AM
#1
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.
-
April 28th, 2010, 06:53 AM
#2
Re: thread safe data
Originally Posted by Moore
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".
-
April 28th, 2010, 09:57 AM
#3
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.)
-
April 28th, 2010, 11:53 AM
#4
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|