What is the fast equivalent of a semaphore Pthread or spin lock?
Background
I am currently using objc_sync_enter / exit pairs to manage locks:
func sync(lock: AnyObject, closure: () -> ())
{
objc_sync_enter(lock)
closure()
objc_sync_exit(lock)
}
func sync<T>(lock: AnyObject, closure: () -> T ) -> T
{
var t : T
objc_sync_enter(lock)
t = closure()
objc_sync_exit(lock)
return t
}
I can use these mechanisms to update the semaphore where I count the tasks that I need to complete.
Now, in a separate thread, I would like to wait until my semaphore reaches zero, perhaps using something like a pthread semaphore or some cheap / lightweight blocking lock?
var semaphore = 2 // ->0 using sync() when all preparation is complete
fun call_me_when_semaphore_becomes_zero() { }
Question
How can I wait for a boolean condition using SWIFT / iOS sync mechanisms?
It would be nice if the interface looked something like this:
// implementation
func sync_wait(lock: AnyObject, condition: (()->Bool), closure: (() -> ()))
{
// NEED HELP HERE -------------------------------------------------------
// using lock, wait until condition becomes true, and then call closure
// ----------------------------------------------------------------------
}
// call
sync_wait( semaphore, condition: { semaphore==0 }, closure: { call_me_when_semaphore_becomes_zero() } )
+3
source to share
No one has answered this question yet
Check out similar questions: