I am looking for an equivalent pattern in multi-threading in iOS as there is in .NET for pulse and wait. Essentially, I want a background thread to lay dormant until a flag is set, which essentially “kicks” the thread into action.
It is an alternative to a loop+thread.sleep(). The flag can be set on a different thread than the actual background thread doing the processing. Thanks!
There are a variety of different mix-and-match thread APIs available on iOS and OS X. What are you using to create your thread?
The simplest suggestion is to use a Grand Central Dispatch (GCD) semaphore.
Setup code:
Thread code:
Trigger code:
An even better suggestion: GCD already manages its own thread pool, so just take advantage of it instead of spinning up your own thread. It’s very easy to use
dispatch_asyncto run some code in a background thread.