• 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: NSCondition (unlocked when not locked)
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSCondition (unlocked when not locked)


  • Subject: Re: NSCondition (unlocked when not locked)
  • From: Benjamin Stiglitz <email@hidden>
  • Date: Fri, 10 Jul 2009 10:53:52 -0400

I have a worker thread that is only slowly polling. I would like to be
able wake him up from a different thread though. A bit like the
select(2) pattern. So what I do is:

...

   pollingCondition = [[NSCondition alloc] init];

...

   - (void) pollNow
   {
       [pollingCondition lock];
       [pollingCondition signal];
       [pollingCondition unlock];
   }

   - (void) workerThread:(id)arg
       ...
       while (!quit) {

           [pollingCondition lock];
           [pollingCondition waitUntilDate: [NSDate
dateWithTimeIntervalSinceNow:SLEEP]];
           [pollingCondition unlock];

           ...
       }
       ...
   }

Is it OK to do it like that? Is there a better way? If the timeout is there only so you can see that quit changed, then you should change your code to either signal the condition to wake up the thread or use a condition lock with three states (no data, data waiting, cancel).

This looks fine modulo an issue I’ll mention below.

I've seen an "unlocked when not locked" and don't understand how this
could happen.

What’s the value of SLEEP? -waitUntilDate: can return NO indicating that the timeout elapsed and the lock is not locked; you probably want -wait.


-Ben

_______________________________________________

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: NSCondition (unlocked when not locked)
      • From: Torsten Curdt <email@hidden>
References: 
 >NSCondition (unlocked when not locked) (From: Torsten Curdt <email@hidden>)

  • Prev by Date: Re: Cocoa Frontend to SDL App
  • Next by Date: Re: Confused about NSPrintInfo margins
  • Previous by thread: NSCondition (unlocked when not locked)
  • Next by thread: Re: NSCondition (unlocked when not locked)
  • Index(es):
    • Date
    • Thread