Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Objective-c > Re: Memory barr...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 4 of 5 Topic 1009 of 1039
Post > Topic >>

Re: Memory barriers in atomic operations

by Greg Parker <gparker@[EMAIL PROTECTED] > Jul 25, 2008 at 12:42 PM

FreeMan <basurero@[EMAIL PROTECTED]
> writes:
> On 2008-07-25 00:55:20 +0200, Greg Parker <gparker@[EMAIL PROTECTED]
> said:
>> Your case of one-writer multiple-readers may or may not need a barrier.
>> It depends what the atomic value is for and how the writer and readers
>> expect it to relate to other values.
>
> Thank you. This is the point in which I am interested in. Assuming that
> only one thread modify the atomic variable: Under which conditions a
> problem/different behavior can arise? Can you provide an example where
> memory barriers solve such problem?

Again, it depends how your atomic variable is related to other variables.

Here's another example. One thread adds items to an array, then 
atomically increments the array count. Other threads are continuously 
reading the array. 

Writer:
    Item *newItem = [[Item alloc] init...];
    array[count] = newItem;
    OSAtomicIncrement(&count);

Reader:
    for (i = 0; i < count; i++) {
        [array[i] doSomething];
    }

This will crash without proper barriers. The reader threads may be able 
to see the updated count before they see the updated array value. The 
garbage value read from the array then causes a crash or other incorrect 
behavior. 

Note that OSAtomicIncrementBarrier() is not enough to fix the problem 
on some architectures; they also need a memory barrier on the reader's 
side to guarantee the desired synchronization.


-- 
Greg Parker     gparker@[EMAIL PROTECTED]
     Runtime Wrangler
 




 5 Posts in Topic:
Memory barriers in atomic operations
FreeMan <basurero@[EMA  2008-07-23 22:22:47 
Re: Memory barriers in atomic operations
Greg Parker <gparker@[  2008-07-24 15:55:20 
Re: Memory barriers in atomic operations
FreeMan <basurero@[EMA  2008-07-25 08:35:03 
Re: Memory barriers in atomic operations
Greg Parker <gparker@[  2008-07-25 12:42:05 
Re: Memory barriers in atomic operations
FreeMan <basurero@[EMA  2008-07-26 23:02:13 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Fri Oct 10 20:10:39 CDT 2008.