• 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: Accessing array in thread safe way
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Accessing array in thread safe way


  • Subject: Re: Accessing array in thread safe way
  • From: Charles Srstka <email@hidden>
  • Date: Thu, 08 Mar 2012 15:20:42 -0600

On Mar 8, 2012, at 2:51 PM, Brian Lambert wrote:

> I should have said, "relatively".  As in, good enough for many scenarios where it will be used infrequently and keeping the code simple makes sense.
>
> Of course, you're right, Charles.
>
> 1,000,000 integer increments synchronized by @synchronized, OSSpinLock, and NSLock:
> PerfTimer[95716:403] 1,000,000 @synchronized ++value: [118 ms] [117,813,038 ns]
> PerfTimer[95716:403] 1,000,000 OSSpinLock ++value: [8 ms] [8,444,567 ns]
> PerfTimer[95716:403] 1,000,000 NSLock ++value: [63 ms] [63,049,168 ns]
>
> A million operations in ~100 ms is fast. A million operations in ~10 ms is WICKED fast.
>
> Brian

Another interesting option is mentioned in Apple’s GCD man pages; dispatch_sync can actually be used as a locking mechanism, like so:

__block NSUInteger value = 0;

dispatch_sync(myLockQueue, ^{
    value++;
});

This results in something almost as simple as @synchronized, and quite a lot faster. Here are the results I get when running it on my MBP. All my results do take about twice as long as yours; I suspect you have a rather nice machine. :-)

2012-03-08 15:14:56.408 Lock Benchmarks[57369:403] @synchronized value++: 259.156942 ms
2012-03-08 15:14:56.429 Lock Benchmarks[57369:403] OSSpinLock value++: 19.486010 ms
2012-03-08 15:14:56.517 Lock Benchmarks[57369:403] pthread_mutex_t value++: 86.329997 ms
2012-03-08 15:14:56.645 Lock Benchmarks[57369:403] NSLock value++: 127.897978 ms
2012-03-08 15:14:56.692 Lock Benchmarks[57369:403] dispatch_sync value++: 45.979023 ms

Not bad; OSSpinLock is the only thing that seems to be able to best it.

Charles


_______________________________________________

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: Accessing array in thread safe way
      • From: Brian Lambert <email@hidden>
References: 
 >Accessing array in thread safe way (From: "Jan E. Schotsman" <email@hidden>)
 >Re: Accessing array in thread safe way (From: Charles Srstka <email@hidden>)
 >Re: Accessing array in thread safe way (From: "Jan E. Schotsman" <email@hidden>)
 >Re: Accessing array in thread safe way (From: CoGe - Tamas Nagy <email@hidden>)
 >Re: Accessing array in thread safe way (From: "Jan E. Schotsman" <email@hidden>)
 >Re: Accessing array in thread safe way (From: Brian Lambert <email@hidden>)
 >Re: Accessing array in thread safe way (From: Charles Srstka <email@hidden>)
 >Re: Accessing array in thread safe way (From: Brian Lambert <email@hidden>)

  • Prev by Date: @-directives
  • Next by Date: Re: Accessing array in thread safe way
  • Previous by thread: Re: Accessing array in thread safe way
  • Next by thread: Re: Accessing array in thread safe way
  • Index(es):
    • Date
    • Thread