On Oct 7, 2016, at 21:06 , Gerriet M. Denkmann <g...@mdenkmann.de> wrote:
> 
> But, alas, it is also much slower: overhead almost 40 sec (whereas my 
> admittedly rather hackish way took less then half a second).

That may indicate a lot of contention — e.g. you were running multiple copies 
of that loop in different threads, and each loop was doing an increment every 
iteration. I’d actually try doing it this way:

>       //      One-time initialization, globally
> 
>       dsema = dispatch_semaphore_create (1);
> 
>       NSUInteger counter = 0;
>       
> 
>       //      Per-thread code, locally
> 
>       NSUInteger perThreadCounter = 0;
> 
>       for …
>       {
>               …
>               perThreadCounter++;
>       }
> 
>       dispatch_semaphore_wait (dsema, <forever>);
>       counter += perThreadCounter;
>       dispatch_semaphore_signal (dsema);

That should diminish the overhead to, effectively, zero.

But this is a bit academic, if you have a non-deprecated method of atomic 
incrementing available. This is about what you might do for a more complicated 
calculation that is not inherently atomic.



_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to