@synchronized
The @synchronized
directive is a convenient way to create mutex locks on the fly in Objective-C code. The @synchronized
directive does what any other mutex lock would do—it prevents different threads from acquiring the same lock at the same time. In this case, however, you do not have to create the mutex or lock object directly. Instead, you simply use any Objective-C object as a lock token, as shown in the following example:
- (void)myMethod:(id)anObj |
{ |
@synchronized(anObj) |
{ |
// Everything between the braces is protected by the @synchronized directive. |
} |
} |
The object passed to the @synchronized
directive is a unique identifier used to distinguish the protected block. If you execute the preceding method in two different threads, passing a different object for the anObj
parameter on each thread, each would take its lock and continue processing without being blocked by the other. If you pass the same object in both cases, however, one of the threads would acquire the lock first and the other would block until the first thread completed the critical section.
As a precautionary measure, the @synchronized
block implicitly adds an exception handler to the protected code. This handler automatically releases the mutex in the event that an exception is thrown. This means that in order to use the @synchronized
directive, you must also enable Objective-C exception handling in your code. If you do not want the additional overhead caused by the implicit exception handler, you should consider using the lock classes.
Objective-C provides support for thread synchronization and exception handling, which are explained in this chapter and in “Exception Handling.” To turn on support for these features, use the -fobjc-exceptions
switch of the GNU Compiler Collection (GCC) version 3.3 and later.
Note: Using either of these features in a program renders the application runnable only in Mac OS X v10.3 and later because runtime support for exception handling and synchronization is not present in earlier versions of the software.
Objective-C supports multithreading in applications. Therefore, two threads can try to modify the same object at the same time, a situation that can cause serious problems in a program. To protect sections of code from being executed by more than one thread at a time, Objective-C provides the @synchronized()
directive.
The @synchronized()
directive locks a section of code for use by a single thread. Other threads are blocked until the thread exits the protected code—that is, when execution continues past the last statement in the @synchronized()
block.
The @synchronized()
directive takes as its only argument any Objective-C object, including self
. This object is known as a mutual exclusion semaphore or mutex. It allows a thread to lock a section of code to prevent its use by other threads. You should use separate semaphores to protect different critical sections of a program. It’s safest to create all the mutual exclusion objects before the application becomes multithreaded, to avoid race conditions.
Listing 12-1 shows code that uses self
as the mutex to synchronize access to the instance methods of the current object. You can take a similar approach to synchronize the class methods of the associated class, using the class object instead of self
. In the latter case, of course, only one thread at a time is allowed to execute a class method because there is only one class object that is shared by all callers.
Listing 12-1 Locking a method using self
- (void)criticalMethod |
{ |
@synchronized(self) { |
// Critical code. |
... |
} |
} |
Listing 12-2 shows a general approach. Before executing a critical process, the code obtains a semaphore from the Account
class and uses it to lock the critical section. The Account
class could create the semaphore in its initialize
method.
Listing 12-2 Locking a method using a custom semaphore
Account *account = [Account accountFromString:[accountField stringValue]]; |
|
// Get the semaphore. |
id accountSemaphore = [Account semaphore]; |
|
@synchronized(accountSemaphore) { |
// Critical code. |
... |
} |
The Objective-C synchronization feature supports recursive and reentrant code. A thread can use a single semaphore several times in a recursive manner; other threads are blocked from using it until the thread releases all the locks obtained with it; that is, every@synchronized()
block is exited normally or through an exception.
When code in an @synchronized()
block throws an exception, the Objective-C runtime catches the exception, releases the semaphore (so that the protected code can be executed by other threads), and rethrows the exception to the next exception handler.
An NSLock
object implements a basic mutex for Cocoa applications. The interface for all locks (including NSLock
) is actually defined by theNSLocking
protocol, which defines the lock
and unlock
methods. You use these methods to acquire and release the lock just as you would any mutex.
In addition to the standard locking behavior, the NSLock
class adds the tryLock
and lockBeforeDate:
methods. The tryLock
method attempts to acquire the lock but does not block if the lock is unavailable; instead, the method simply returns NO
. The lockBeforeDate:
method attempts to acquire the lock but unblocks the thread (and returns NO
) if the lock is not acquired within the specified time limit.
The following example shows how you could use an NSLock
object to coordinate the updating of a visual display, whose data is being calculated by several threads. If the thread cannot acquire the lock immediately, it simply continues its calculations until it can acquire the lock and update the display.
BOOL moreToDo = YES; |
NSLock *theLock = [[NSLock alloc] init]; |
... |
while (moreToDo) { |
/* Do another increment of calculation */ |
/* until there’s no more to do. */ |
if ([theLock tryLock]) { |
/* Update display used by all threads. */ |
[theLock unlock]; |
} |
} |