Re: How to count Atoms

2016-10-10 Thread Greg Parker
> On Oct 7, 2016, at 9:06 PM, Gerriet M. Denkmann wrote: > > My preferred way to count (not deprecated and fast, but, as you said, > probably not available in Swift) is: > > #import > atomic_uint_fast64_t counter; > atomic_fetch_add_explicit( , 1,

Re: How to count Atoms

2016-10-08 Thread Jens Alfke
> On Oct 7, 2016, at 10:05 AM, Quincey Morris > wrote: > > Surely “anyone reading the code” is going to recognize “dispatch_semaphore” > as something to do with a semaphore, including people from other platforms? You’re right; I was [mis]remembering a

Re: How to count Atoms

2016-10-07 Thread Gerriet M. Denkmann
> On 8 Oct 2016, at 11:22, Quincey Morris > wrote: > > On Oct 7, 2016, at 21:06 , Gerriet M. Denkmann wrote: >> >> But, alas, it is also much slower: overhead almost 40 sec (whereas my >> admittedly rather hackish way took less then

Re: How to count Atoms

2016-10-07 Thread Quincey Morris
On Oct 7, 2016, at 21:06 , Gerriet M. Denkmann 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

Re: How to count Atoms

2016-10-07 Thread Gerriet M. Denkmann
> On 8 Oct 2016, at 05:01, Quincey Morris > wrote: > > On Oct 7, 2016, at 07:49 , Gerriet M. Denkmann wrote: >> >> Is there a better way than this: >> dsema = dispatch_semaphore_create( 0 ); >> >> some loop to be counted

Re: How to count Atoms

2016-10-07 Thread Quincey Morris
On Oct 7, 2016, at 15:37 , Dave Fernandes wrote: > > But I don’t see how incrementing it after creating it is any different from > creating it with a non-zero count. Either way, if you have resources > available, the count will be non-zero, and you will crash if you

Re: How to count Atoms

2016-10-07 Thread Dave Fernandes
> > In practice, you’d actually initialize the semaphore like this: > >> dsema = dispatch_semaphore_create (0); // start with a zero count >> dispatch_semaphore_signal (dsema); // increment to the number of >> resources in the pool. > > That’s because if you create the semaphore with

Re: How to count Atoms

2016-10-07 Thread Quincey Morris
On Oct 7, 2016, at 07:49 , Gerriet M. Denkmann wrote: > > Is there a better way than this: > dsema = dispatch_semaphore_create( 0 ); > > some loop to be counted > { > dispatch_semaphore_signal(dsema); > …. > } > > NSUInteger counter = 0; >

Re: How to count Atoms

2016-10-07 Thread Kyle Sluder
On Fri, Oct 7, 2016, at 11:24 AM, Jens Alfke wrote: > > > On Oct 7, 2016, at 1:14 AM, Quincey Morris > > wrote: > > > > One straightforward way is to use dispatch_semaphore. IIRC it’s lightweight > > unless it blocks (that is, unless its count is zero

Re: How to count Atoms

2016-10-07 Thread Quincey Morris
On Oct 7, 2016, at 09:24 , Jens Alfke wrote: > > IMO dispatch_semaphore is overkill for this; it’s not just an atomic counter, > it’s also got blocking behaviors for use in e.g. managing a pool of resources. I wasn’t suggesting using dispatch_semaphore itself as the atomic

Re: How to count Atoms

2016-10-07 Thread Jens Alfke
> On Oct 7, 2016, at 1:14 AM, Quincey Morris > wrote: > > One straightforward way is to use dispatch_semaphore. IIRC it’s lightweight > unless it blocks (that is, unless its count is zero when you wait), so it’s > good for this situation where actual

Re: How to count Atoms

2016-10-07 Thread Alastair Houghton
On 7 Oct 2016, at 08:19, Gerriet M. Denkmann wrote: > So what is the proper way to count something atomicly and undeprecatedly? or is the approved source for this kind of thing. In C++, you might write #include std::atomic counter; then you can just do

Re: How to count Atoms

2016-10-07 Thread Gerriet M. Denkmann
> On 7 Oct 2016, at 15:14, Quincey Morris > wrote: > > On Oct 7, 2016, at 00:19 , Gerriet M. Denkmann wrote: >> >> So what is the proper way to count something atomicly and undeprecatedly? > > One straightforward way is to use

Re: How to count Atoms

2016-10-07 Thread Gerriet M. Denkmann
> On 7 Oct 2016, at 15:43, Ken Thomases wrote: > > On Oct 7, 2016, at 2:19 AM, Gerriet M. Denkmann wrote: >> >> I need (just for debugging purposes) to count something in a thread safe >> way. >> […] >> So I tried OSIncrementAtomic. >> Now I get:

Re: How to count Atoms

2016-10-07 Thread Ken Thomases
On Oct 7, 2016, at 2:19 AM, Gerriet M. Denkmann wrote: > > I need (just for debugging purposes) to count something in a thread safe way. > […] > So I tried OSIncrementAtomic. > Now I get: "Implicit declaration of function 'OSIncrementAtomic' is invalid > in C99" and the

Re: How to count Atoms

2016-10-07 Thread Quincey Morris
On Oct 7, 2016, at 00:19 , Gerriet M. Denkmann wrote: > > So what is the proper way to count something atomicly and undeprecatedly? One straightforward way is to use dispatch_semaphore. IIRC it’s lightweight unless it blocks (that is, unless its count is zero when you wait),

How to count Atoms

2016-10-07 Thread Gerriet M. Denkmann
I need (just for debugging purposes) to count something in a thread safe way. This works, but is deprecated: SInt32 counter; - (IBAction)doSomething:sender { counter = 0; some loop { IncrementAtomic( );// 'IncrementAtomic' is