• 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: How to count Atoms
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to count Atoms


  • Subject: Re: How to count Atoms
  • From: Quincey Morris <email@hidden>
  • Date: Fri, 07 Oct 2016 15:01:23 -0700
  • Feedback-id: 167118m:167118agrif8a:167118s1y6_KlIuo:SMTPCORP

On Oct 7, 2016, at 07:49 , Gerriet M. Denkmann <email@hidden> 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;
> for(;;)
> {
> 	long a = dispatch_semaphore_wait(dsema, DISPATCH_TIME_NOW);
> 	if ( a != 0 ) break;
> 	counter++;
> };

Er, I didn’t pay enough attention to this when you posted. No, this isn’t the correct approach. You’re supposed to be using the semaphore as a lock. In theory, you’d do it like this:

> 	dsema = dispatch_semaphore_create (1);
>
> 	NSUInteger counter = 0;
>
> 	for …
> 	{
> 		…
> 		dispatch_semaphore_wait (dsema, <forever>);
> 		counter++;
> 		dispatch_semaphore_signal (dsema);
> 	}

That is, the semaphore controls access to a pool of 1 resources (the resource being permission to increment the counter), and you wait on the resource to become available (“lock”), increment the counter, then release the resource (“unlock”).

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 a non-zero count, then later try to release the semaphore object when its count is something different (which can happen if you have a more complex loop that you break out of), your app will crash. The API design assumption is that if you didn’t free all the original resources, your app has a bug (which I happen to think is bogus reasoning, but anyway…).


_______________________________________________

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: How to count Atoms
      • From: "Gerriet M. Denkmann" <email@hidden>
    • Re: How to count Atoms
      • From: Dave Fernandes <email@hidden>
References: 
 >How to count Atoms (From: "Gerriet M. Denkmann" <email@hidden>)
 >Re: How to count Atoms (From: Quincey Morris <email@hidden>)
 >Re: How to count Atoms (From: "Gerriet M. Denkmann" <email@hidden>)

  • Prev by Date: Directory enumeration gives wrong file type for aliases
  • Next by Date: Re: Directory enumeration gives wrong file type for aliases
  • Previous by thread: Re: How to count Atoms
  • Next by thread: Re: How to count Atoms
  • Index(es):
    • Date
    • Thread