I am transferring a program from java to Objective C and need to use wait and notify methods that are frequently used for threading in java but can't seem to find any good equivalent in Objective C. I've tried using NSLock object but I don't think it is working. (I'm using [NSLock lock] for waits and [NSLock unlock] for notifies) Is there any good equivalent in Objective C that I haven't been able to find?
Hence, wait() and notify() methods are defined in Object class rather than Thread class. If wait() and notify() were on the Thread instead then each thread would have to know the status of every other thread and there is no way to know thread1 that thread2 was waiting for any resource to access.
The wait() method causes the current thread to wait until another thread invokes the notify() or notifyAll() methods for that object. The notify() method wakes up a single thread that is waiting on that object's monitor. The notifyAll() method wakes up all threads that are waiting on that object's monitor.
No! Only option is to wait with a timeout, which surely will not help you.
There are numerous techniques you could use. You can use NSCondition or POSIX semaphores or dispatch semaphores or by using run loops. Check out the Concurrency Guide and the the Threading Guide.
My personal favourite at the moment is the dispatch semaphore.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With