ios - What is swift's equivalent of a Pthread semaphore or a spin lock? -
background
i using objc_sync_enter/exit pairs 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 update semaphore count down tasks need complete.
now, in separate thread, wait until semaphore reaches zero, perhaps using pthread semaphore or kind of cheap/light spin lock?
var semaphore = 2 // ->0 using sync() when preparation complete fun call_me_when_semaphore_becomes_zero() { }
question
how can wait boolean condition using swift/ios synchronization mechanisms?
it nice if interface looked this:
// implementation func sync_wait(lock: anyobject, condition: (()->bool), closure: (() -> ())) { // need here ------------------------------------------------------- // using lock, wait until condition becomes true, , call closure // ---------------------------------------------------------------------- } // call sync_wait( semaphore, condition: { semaphore==0 }, closure: { call_me_when_semaphore_becomes_zero() } )
Comments
Post a Comment