Class: Dispatch::Semaphore
Overview
Dispatch::Semaphore provides an efficient mechanism to synchronizes threads via a combination of waiting and signalling. This is especially useful for controlling access to limited resources.
Class Method Summary (collapse)
Instance Method Summary (collapse)
-
- Dispatch::Semaphore.new
constructor
Returns a Semaphore used to synchronize threads through a combination of waiting and signalling, as detailed in the dispatch_semaphore_create(3) man page.
-
- signal
Signals the semaphore to wake up any waiting threads.
-
- wait
Waits (blocks the thread) until a signal arrives or the timeout expires.
Methods inherited from Object
#dispatch_object, #resume!, #suspend!, #suspended?
Methods inherited from NSObject
#!, #!=, #!~, #<=>, #==, #===, #=~, #Rational, #__callee__, #__method__, #__send__, #__type__, `, allocWithZone:, #autoContentAccessingProxy, autoload, autoload?, autorelease_pool, #awakeAfterUsingCoder:, binding, block_given?, caller, cancelPreviousPerformRequestsWithTarget:, cancelPreviousPerformRequestsWithTarget:selector:object:, catch, class, classFallbacksForKeyedArchiver, #classForCoder, #classForKeyedArchiver, classForKeyedUnarchiver, #clone, conformsToProtocol:, #copy, copyWithZone:, #dealloc, #define_singleton_method, description, display, #doesNotRecognizeSelector:, #dup, #enum_for, #eql?, #equal?, #extend, fail, #finalize, format, #forwardInvocation:, #forwardingTargetForSelector:, framework, #freeze, #frozen?, getpass, gets, global_variables, #init, initialize, #initialize_clone, #initialize_copy, #initialize_dup, #inspect, instanceMethodForSelector:, instanceMethodSignatureForSelector:, #instance_eval, #instance_exec, #instance_of?, #instance_variable_defined?, #instance_variable_get, #instance_variable_set, #instance_variables, instancesRespondToSelector:, isSubclassOfClass:, #is_a?, iterator?, #kind_of?, lambda, load, load_bridge_support_file, load_plist, local_variables, loop, #method, #methodForSelector:, #methodSignatureForSelector:, #methods, #mutableCopy, mutableCopyWithZone:, new, #nil?, open, p, #performSelector:onThread:withObject:waitUntilDone:, #performSelector:onThread:withObject:waitUntilDone:modes:, #performSelector:withObject:afterDelay:, #performSelector:withObject:afterDelay:inModes:, #performSelectorInBackground:withObject:, #performSelectorOnMainThread:withObject:waitUntilDone:, #performSelectorOnMainThread:withObject:waitUntilDone:modes:, print, printf, #private_methods, proc, #protected_methods, #public_method, #public_methods, #public_send, putc, puts, raise, rand, readline, readlines, #replacementObjectForCoder:, #replacementObjectForKeyedArchiver:, require, resolveClassMethod:, resolveInstanceMethod:, #respond_to?, #respond_to_missing?, select, #send, setVersion:, #singleton_methods, sprintf, srand, superclass, #taint, #tainted?, #tap, test, throw, #to_plist, #to_s, trace_var, trap, #trust, #untaint, untrace_var, #untrust, #untrusted?, version
Constructor Details
- (Dispatch::Semaphore) Dispatch(count)
Returns a Semaphore used to synchronize threads through a combination of waiting and signalling, as detailed in the dispatch_semaphore_create(3) man page.
If the count parameter is equal to zero, the semaphore is useful for synchronizing completion of work:
gcdq = Dispatch::Queue.new('doc')
sema = Dispatch::Semaphore.new(0)
gcdq.async { puts "Begin."; sema.signal }
puts "Waiting..."
sema.wait
puts "End!"
If the count parameter is greater than zero, then the semaphore is useful for managing a finite pool of resources.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class NSObject
Class Method Details
+ (Object) alloc
Instance Method Details
- (Boolean) signal
Signals the semaphore to wake up any waiting threads
Returns true if no thread is waiting, false otherwise
gcdq = Dispatch::Queue.new('doc')
sema = Dispatch::Semaphore.new(0)
gcdq.async {
sleep 0.1
sema.signal #=> false
}
sema.wait
- (Boolean) wait(timeout)
Waits (blocks the thread) until a signal arrives or the timeout expires. Timeout defaults to DISPATCH_TIME_FOREVER.
Returns true if signalled, false if timed out.
gcdq = Dispatch::Queue.new('doc')
sema = Dispatch::Semaphore.new(0)
gcdq.async { sleep 0.1; sema.signal }
sema.wait #=> true