Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wait and notify equivalent in Objective c?

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?

like image 926
pongahead Avatar asked Jun 03 '11 15:06

pongahead


People also ask

Why wait and notify in object class?

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.

What is difference between wait and notify?

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.

Can we use wait without notify?

No! Only option is to wait with a timeout, which surely will not help you.


1 Answers

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.

like image 176
JeremyP Avatar answered Oct 10 '22 21:10

JeremyP