Multithreading (IOS) Study Notes

Source: Internet
Author: User
Tags posix

1. In addition to the lock, the system also provides conditions to ensure the proper sequence of execution of tasks in your application. A piece serves as a gatekeeper, blocking a given thread until it represents a true condition. In this case, the condition releases the thread and allows it to continue execution.

(If you use an operation object, you can configure the dependency order between your operation objects to determine the task execution sequence, which is very similar to the behavior provided by the condition ).

2. You can use the applicationshouldterminate: Delegate method to delay the interruption of the program until a period of time or the cancellation is completed. When the delay is interrupted, your program needs to wait until all cycle threads (connectable threads) have completed their tasks and called the replytoapplicationshouldterminate: method. For more information about these methods, refer
Nsapplication class reference.
3. The minimum allowed stack size for secondary threads is 16 KB and the stack size must be a multiple of 4 kb

512 KB (secondary threads) 8 MB (Mac OS X main thread) 1 MB (IOS main thread)
4. For multi-threaded applications, the cocoa framework uses locks and other Synchronization Methods to ensure correct code execution. To prevent these locks from causing performance loss in a single thread, cocoa creates these locks until the application uses the nsthread class to generate its first new thread. Therefore, when you only use POSIX routines to generate new threads, cocoa will not receive notifications about the current multi-thread change of your application. To let cocoa know that you are planning to use multithreading, what you need to do is to use the nsthread class to generate
A thread and let it exit immediately. You only need to use nsthread to generate a thread, which is enough to ensure that the lock required by the cocoa framework is in place. You can use the ismultithreaded method of nsthread to check the problem.

5. Each thread maintains a key-value dictionary, which can be accessed anywhere in the thread. You can use this dictionary to save some information, which remains unchanged throughout the thread's execution process.
In cocoa, you use the threaddictionary method of nsthread to retrieve an nsmutabledictionary object. You can add the keys required by any thread in it. In POSIX,
You use the pthread_setspecific and pthread_getspecific functions to set and access the keys and values of your thread.

6. A connection line can pass a Data Pointer or other return values to the pthread_exit function. Other threads can obtain the data through the pthread_join function.

Important: When an application exits, the disconnection from the thread can be immediately interrupted, but the thread that can be connected cannot. Each connectable thread must be connected when the process is allowed to exit. Therefore, when the thread is working cyclically and cannot be interrupted, such as saving data to the hard disk, it is the best choice to connect to the thread. See 2

The only way to create a connectable thread is to use a POSIX thread. By default, the threads created by POSIX are connectable. To mark the thread as disconnected or connectable, use the pthread_attr_setdetachstate function to modify the attributes of the thread being created. After the thread starts,
You can call the pthread_detach function to change the detached thread to connectable.
Change the thread priority. Cocoa threads use nsthread's setthreadpriority: class method. POSIX thread, using the pthread_setschedparam function.

7. When you write the thread main entry, the first thing is to create an automatic release pool. Installing a try/catch module at the entry point of the main body of your thread allows you to capture any unknown exceptions and provide a suitable response.
8. When selector is executed on other threads, the target thread must have an active run loop. To add an observer to the run loop, you can create a cfrunloopobserverref opacity class.
And use cfrunloopaddobserver to add it to your run loop. Run
The loop observer must be created by the core foundation function, even the cocoa Program

Exit run Loop

There are two ways to exit the run loop before processing the event: Set a timeout value for the run loop to notify the run loop to stop, and use cfrunloopstop to explicitly stop the run loop.
Loop

9. If multiple threads run at the same time, the signal is transmitted to the thread selected by any system. In other words, the signal can be transmitted to any thread of your application. The first rule for implementing signal processing in your application is to avoid the assumption that any thread processes the signal.
10. A deadlock occurs when two different threads keep one lock (which is required by another thread) and attempt to obtain the lock maintained by another thread.
A live lock is similar to a deadlock. When two threads compete for the same resource, they may issue a live lock. In the case of a life lock, a thread gives up its first lock and tries to obtain the second lock. Once it gets the second lock, it returns and tries to get a lock again. The thread is locked because it takes all the time to release a lock and tries to obtain other locks without actually working.

The best way to avoid deadlocks and live locks is to have only one lock at a time. If you must obtain more than one lock at a time, make sure that other threads do not do similar things.

11. @ synchronized is a convenient method to create a mutex lock in the objective-C code.

As a precaution, @ synchronized implicitly adds an exception handling routine to protect the code.

This processing routine Automatically releases the mutex when an exception is thrown. This means that to use the @ synchronized command, you must enable Exception Handling in your code.

The lock defined by the nsrecursivelock class can be obtained multiple times in the same thread without causing a deadlock.

12. The nsconditionlock object defines a mutex lock, which can be locked and unlocked with specific values. Do not confuse the locks and conditions of this type (see the "condition" Section): Lock unlock
Nscondition: Lock wait signal unlock when multiple threads need to execute tasks in a specific order, you can use an nsconditionlock object, for example, when a thread produces data, another thread consumes data. When the producer executes the lock, the consumer uses the condition specified by your program to obtain the lock (the condition itself is an integer value defined by you ). When the producer completes, it will unlock the lock and set the lock condition to an appropriate integer value to wake up the consumer.
Thread, and then the consumption thread continues to process data.

13. The nsdistributedlock class can be used by multiple applications on multiple hosts to restrict access to some shared resources, such as a file.

 


Related Article

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.