• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Lockless thread-safe accessor using blocks: how to?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Lockless thread-safe accessor using blocks: how to?


  • Subject: Re: Lockless thread-safe accessor using blocks: how to?
  • From: David Duncan <email@hidden>
  • Date: Thu, 14 Apr 2011 10:44:04 -0700

On Apr 14, 2011, at 10:26 AM, WT wrote:

> On Apr 14, 2011, at 2:09 PM, David Duncan wrote:
>
>> On Apr 14, 2011, at 10:02 AM, WT wrote:
>>
>>> I looked at dispatch_once() at one point, but I'm still confused by how it works.
>>
>> dispatch_once uses a predicate (just a flag) to determine if it should run the block or not. If that flag is false, then it sets the flag to true and executes the block. If the flag is true, it does nothing. What makes dispatch_once useful over a simple if statement is that it ensures that if you execute dispatch_once concurrently from multiple threads that flag gets updated exactly once, and the block gets called exactly once.
>
> Thanks for explaining it, David. The bit that was throwing me off is the predicate part. I understand now that all I need is to declare a static variable of the appropriate type and pass a pointer to it to dispatch_once().

The lifetime of the predicate needs to match that of the initialization done by the block. If the block's initialization is done for an instance variable, then the predicate should also be an instance variable. If the initialization is for a global, the predicate should also be a global.

> From my understanding of dispatch_once(), I can't use it for this purpose because it really executes the block only once in the application's life time.


There are complications in doing this due to the fact that resetting the predicate may introduce race conditions. But then as DaveZ mentioned, your pattern can introduce deadlocks too. I don't think there is a good pattern for doing exactly what you want to do with the conditions you impose on it, but I suspect if you re-evaluate your design you can find a better solution with fewer issues.
--
David Duncan

_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: Lockless thread-safe accessor using blocks: how to?
      • From: Jason Harris <email@hidden>
    • Re: Lockless thread-safe accessor using blocks: how to?
      • From: WT <email@hidden>
References: 
 >Re: Lockless thread-safe accessor using blocks: how to? (From: Jonathan Taylor <email@hidden>)
 >Re: Lockless thread-safe accessor using blocks: how to? (From: WT <email@hidden>)
 >Re: Lockless thread-safe accessor using blocks: how to? (From: Jonathan Taylor <email@hidden>)
 >Re: Lockless thread-safe accessor using blocks: how to? (From: David Duncan <email@hidden>)
 >Re: Lockless thread-safe accessor using blocks: how to? (From: WT <email@hidden>)
 >Re: Lockless thread-safe accessor using blocks: how to? (From: David Duncan <email@hidden>)
 >Re: Lockless thread-safe accessor using blocks: how to? (From: WT <email@hidden>)

  • Prev by Date: Re: Lockless thread-safe accessor using blocks: how to?
  • Next by Date: Re: Cocoa witch's broom
  • Previous by thread: Re: Lockless thread-safe accessor using blocks: how to?
  • Next by thread: Re: Lockless thread-safe accessor using blocks: how to?
  • Index(es):
    • Date
    • Thread