Torsten Curdt wrote:

        while (!quit) {

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

            ...
        }
        ...
    }

Is it OK to do it like that? Is there a better way?


You're not following the pattern laid out in the Overview setion of the NSCondition class reference. In particular, you don't have a boolean predicate, and you're not doing the task's work between the bounds of lock and unlock.

http://developer.apple.com/documentation/Cocoa/Reference/ NSCondition_class/Reference/Reference.html

"A boolean predicate is an important part of the semantics of using conditions because of the way signaling works. Signaling a condition does not guarantee that the condition itself is true. There are timing issues involved in signaling that may cause false signals to appear. Using a predicate ensures that these spurious signals do not cause you to perform work before it is safe to do so. The predicate itself is simply a flag or other variable in your code that you test in order to acquire a Boolean result."

Also, quoted from the "Threading Programming Guide" linked to from NSCondition's class reference.

"The correct implementation of conditions requires careful coding, however, so you should look at the examples in “Using Conditions” before using them in your own code."

  -- GG

_______________________________________________

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