1. Synchronized this is mainly a multi-threaded program, this directive can restrict the code within {} to one thread, and if a thread is not finished, the other threads will have to wait if they need to execute.
OBJECTIVE-C provides thread synchronization capabilities in addition to exception handling mechanisms. Exception handling is described in the previous "Exception handling" section.
The role of @synchronized is to create a mutex that ensures that no other thread modifies the self object at this time. This is a lock token for objective-c, which prevents the self object from being accessed by other threads at the same time, and acts as a thread protection. Generally used in the case of common variables, such as singleton mode or the static variable of an operation class.
1. Synchronized this is mainly a multi-threaded program, this directive can restrict the code within {} to one thread, and if a thread is not finished, the other threads will have to wait if they need to execute.
OBJECTIVE-C provides thread synchronization capabilities in addition to exception handling mechanisms. Exception handling is described in the previous "Exception handling" section.
Synchronous execution of Threads
Objective-c Multithreading in the support program. This means that two threads are likely to modify the same object at the same time, which can cause serious problems in the program. To prevent this multiple threads from executing the same piece of code at the same time, OBJECTIVE-C provides the @synchronized () directive.
Instruction @synchronized () is locked by the use of a piece of code. Other threads that attempt to execute the code are blocked until the locking thread exits executing the section of the protected snippet, which means that the last statement in the @synchronized () code block has been executed.
Directive @synchronized () requires a parameter. This parameter can make any Objective-c object, including self. This object is the mutex signal volume. He was able to have a thread protect a piece of code from other threads executing the code. For the different critical code segments in the program, we should use different semaphores separately. It is only safe to create all the required mutex objects to avoid contention between threads, before the application program executes multithreading.
The code in Listing 12-1 uses self as the mutex to achieve synchronization of the current object's access to the instance method.
- -(void) Criticalmethod
- {
- @synchronized (self)
- {
- Key code;
- }
- }
Listing 12-2 using a custom semaphore to lock the method
- Account *account = [accountaccoutfromstring: [accountfiled StringValue]];
- Get the semaphore
- ID Accountsemaphore = [account semaphore];
- @synchronized (Accountsemaphore)
- {
- Key code
- }
The synchronization feature in Objective-c supports recursion. A thread can use the same semaphore multiple times in a recursive manner; the other threads are blocked knowing that the thread has released all of its own locks associated with that semaphore, meaning that it exits all @synchronized () blocks of code through normal execution or by exception handling.
When an exception is thrown in the @synchronized () code block, the OBJECTIVE-C runtime catches the exception and releases the semaphore and throws the exception back to the next exception handler.
Usage of IOS----------@synchronized (self)