Multithreading (IOS) study notes

1, in addition to the lock, the system also provides the conditions to ensure proper order for your application to perform the task. A condition as a gatekeeper, blocking a given thread, the representative of the condition until it becomes true. When this happens, the conditions for the release of the thread and allow it to continue

 (If you use operating objects, you can configure the order dependencies between your operation target execution order of tasks, which provide the conditions and behavior are very similar).

  2, by using   applicationShouldTerminate:   to delay the process of delegate methods or interrupted until after a period of time to complete the cancellation. When the delay interrupt, you need to wait until the program period any thread (the thread can be connected) have completed their task and calls   replyToApplicationShouldTerminate: method. For more information about these methods, see 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 multithreaded applications, Cocoa frameworks use locks and other synchronization methods to ensure the proper execution of code lines. In order to prevent these losses in the single-thread locked inside performance, Cocoa applications use until   NSThread   class to generate its first new thread when it created these locks.
So when you use only   POSIX   to create a new thread routines, Cocoa will not receive on your current application becomes multithreaded notice, in order to let   Cocoa   know that you are planning to use multiple threads, you need to do is to use   NSThread   class generates a thread and let it withdraw immediately, just use   NSThread   to generate a thread would be sufficient to ensure   Cocoa   desired frame lock in place.
You can use   NSThread   of   isMultiThreaded   way to test it.

5, each thread maintains a key - value dictionary, which can be accessed from anywhere inside the thread. You can use this dictionary to store some information that remains constant throughout the execution thread.

In   Cocoa   inside, you use   NSThread   of   threadDictionary   method to retrieve a   NSMutableDictionary   object, you can add any key thread needed in it. In   POSIX   inside, you use   pthread_setspecific   and   pthread_getspecific   functions to set and access keys and values your thread.

 6, can be connected to the thread can pass a pointer or other return data value to the  pthread_exit  function. Other threads can  pthread_join  to get these data functions.

 Important: When the application exits, from the thread can be interrupted immediately, and can be connected to the thread can not. Each thread can be connected must be connected when the process is allowed to exit. So when the thread is not allowed to work periodically interrupted, such as saving data to the hard disk can be connected to the thread is the best choice. See 2

Create a connection thread, the only way is to use   POSIX   threads. POSIX threads are created by default connection. In order to mark off the thread or using connectable   pthread_attr_setdetachstate   functions to modify the properties of threads that are created. After the thread is started, you can call   pthread_detach   to put out the thread of connection can be modified to function.

Change the thread priority, Cocoa   threads   NSThread   of   setThreadPriority: class method. POSIX threads, use pthread_setschedparam function.        

7, when you write thread main entrance of the first things is to create an auto-release pool.

Install one of the main entry point of the thread you   try / catch   block allows you to capture any unknown exceptions and provide an appropriate response.

8, when performed in other threads above the  selector  , the target thread must have an active  run loop. 

 To  run loop  to add an observer, you can create  CFRunLoopObserverRef  opaque type and use  CFRunLoopAddObserver  to add it to your  run loop. Run loop  observers must be a  Core foundation  created function, even  Cocoa  program

Exit  Run Loop

There are two ways to make   run loop   to handle events before exiting:
  to   run loop   to set the timeout
  notify the   run loop   to stop, using   CFRunLoopStop   explicitly stop   run loop


9, if multiple threads simultaneously running, a signal is transmitted to any system selected thread. In other words, the signal can be passed to any thread of your application.

The first rule for signal processing applications in which you assume any of a thread is to avoid processed signal

10, when two different threads are keeping a lock (while the lock is another thread needed) tried to get another deadlock occurs when a thread holds the lock.

  活锁和死锁类似,当两个线程竞争同一个资源的时候就可能发生活锁。在发 生活锁的情况里,一个线程放弃它的第一个锁并试图获得第二个锁。一旦它获得第二 个锁,它返回并试图再次获得一个锁。线程就会被锁起来,因为它花费所有的时间来 释放一个锁,并试图获取其他锁,而不做实际的工作。

 避免死锁和活锁的最好方法是同一个时间只拥有一个锁。如果你必须在同一时间 获取多于一个锁,你应该确保其他线程没有做类似的事情

 11、@synchronized  指令是在 Objective-C 代码中创建一个互斥锁非常方便的方法。

作为一种预防措施,@synchronized 块隐式的添加一个异常处理例程来保护代码。

该处理例程会在异常抛出的时候自动的释放互斥锁。这意味着为了使用  @synchronized  指令,你必须在你的代码中启用异常处理

NSRecursiveLock  类定义的锁可以在同一线程多次获得,而不会造成死锁

12、NSConditionLock  对象定义了一个互斥锁,可以使用特定值来锁住和解锁。不要 把该类型的锁和条件(参见“条件”部分)混淆了:lock unlock
而NSCondition:lock wait signal unlock
When you need to perform tasks in a specific order multiple threads, you can use a  NSConditionLock  objects, such as when a thread production data, while another thread consumption data. When producers execution, consumer use conditions specified by your program to get the lock (condition itself is an integer value that you define). When a producer is completed, it will lock and unlock the lock setting conditions for an appropriate integer value to wake up the consumer thread after thread consumption continues to process data.

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

 


   

Reproduced in: https: //my.oschina.net/dake/blog/196783

Guess you like

Origin blog.csdn.net/weixin_34289454/article/details/91586431