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

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

Reply via email to