Usage of IOS----------@synchronized (self)

Source: Internet
Author: User

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.

 
    1. -(void) Criticalmethod
    2. {
    3. @synchronized (self)
    4. {
    5. Key code;
    6. }
    7. }

Listing 12-2 using a custom semaphore to lock the method

 
    1. Account *account = [accountaccoutfromstring: [accountfiled StringValue]];
    2. Get the semaphore
    3. ID Accountsemaphore = [account semaphore];
    4. @synchronized (Accountsemaphore)
    5. {
    6. Key code
    7. }

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.