Multithreading and network development for iOS: NSThread and iosnsthread
 
Master haomeng is devoted to his contribution and respects the author's Labor achievements. Do not repost them.
 
If the article is helpful to you, you are welcome to donate to the author, support haomeng master, the amount of donation is free, focusing on your mind ^_^
 
I want to donate: Click to donate
 
Cocos2d-X source code download: point I send
 
Game official download: http://dwz.cn/RwTjl
 
Game video preview: http://dwz.cn/RzHHd
 
Game Development blog: http://dwz.cn/RzJzI
 
Game source code transfer: Http://dwz.cn/Nret1
 
A. basic use of NSThread 1. Create and start A thread. An NSThread object represents A thread. 
 
Create and start a thread 
NSThread * thread = [[NSThread alloc] initWithTarget: self selector: @ selector (run) object: nil]; 
[Thread start]; 
// When the thread starts, the run method of self will be executed in the thread 
 
Main thread usage 
+ (NSThread *) mainThread; // obtain the main thread 
-(BOOL) isMainThread; // whether the master thread is used 
+ (BOOL) isMainThread; // determines whether the master thread is used. 2. Obtain the current thread using other methods. 
NSThread * current = [NSThread currentThread]; 
 
Thread Scheduling Priority 
+ (Double) threadPriority; 
+ (BOOL) setThreadPriority :( double) p; 
-(Double) threadPriority; 
-(BOOL) setThreadPriority :( double) p; 
The value range of the scheduling priority is 0.0 ~ 1.0. The default value is 0.5. A greater value indicates a higher priority. 
 
Thread name 
-(Void) setName :( NSString *) n; 
-(NSString *) name; 3. threads are automatically started after other thread creation methods are created. 
[NSThread detachNewThreadSelector: @ selector (run) toTarget: self withObject: nil]; 
 
Implicitly create and start a thread 
[Self defined mselectorinbackground: @ selector (run) withObject: nil]; 
 
Advantages and disadvantages of the above two thread creation methods 
Advantage: simple and quick 
Disadvantage: the thread cannot be set in more detail. 4. Thread lifecycle. 5. Control the thread turntable startup thread. 
-(Void) start; 
// Enter the ready status-> running status. When the thread task is completed, it automatically enters the dead state. 
 
Blocking (pausing) threads 
+ (Void) sleepUntilDate :( NSDate *) date; 
+ (Void) sleepForTimeInterval :( NSTimeInterval) ti; 
// Enter the blocking status 
 
Force stop thread 
+ (Void) exit; 
// Enters the dead state 
 
Note: Once the thread stops (dead), the task cannot be started again. 6. multi-thread security risk Resource Sharing 
One resource may be shared by multiple threads, that is, multiple threads may access the same resource. 
For example, multiple threads access the same object, the same variable, and the same file. 
 
When multiple threads access the same resource, it is easy to cause data disorder and data security problems 7. mutex lock (to prevent multi-threaded resource snatching) (1) mutex lock format 
@ Synchronized (Lock Object) {// code to be locked} 
Note: Only one lock is used to lock one piece of code. Multiple locks are invalid. 
 
Advantages and disadvantages of mutex lock 
Advantage: it can effectively prevent data security problems caused by multi-thread resource grabbing. 
Disadvantage: it consumes a lot of CPU resources. 
 
Prerequisite for mutex lock: multiple threads snatch the same resource 
 
Related terminology: Thread Synchronization 
Thread Synchronization means that multiple threads execute tasks in sequence. 
The mutex lock uses the thread synchronization technology (2) Locking @ synchronized @ property (nonatomic, strong) NSObject * lock; self. lock = [[NSObject alloc] init] @ synchronized (self. lock ){...} note: you must use the same "Lock" to properly lock code resources. Advantages: effectively prevents multiple threads from snatching resources. Disadvantages: when a large amount of CPU resources are consumed, the member attribute modifier atomic is locked, this is the default one. Therefore, we generally use nonatomic to improve the performance and avoid multi-thread resource grabbing. We try to hand over the resource Grabbing Process to the server for processing (3) the atomic and nonatomic modifiers include nonatomic and atomic when defining attributes. 
Atomic: atomic attribute, locking the setter method (the default value is atomic) 
Nonatomic: Non-atomic attribute. It does not lock the setter method. 
 
Atomic Locking Principle 
@ Property (assign, atomic) int age; 
-(Void) setAge :( int) age 
{ 
@ Synchronized (self ){ 
_ Age = age; 
} 
} (4) Comparison between atomic vs nonatomicnonatomic and atomic 
Atomic: thread security, which consumes a lot of resources 
Nonatomic: Non-thread-safe, suitable for mobile devices with small memory 
 
IOS development suggestions 
All attributes are declared as nonatomic 
Avoid using multiple threads to snatch the same resource. 
The Service Logic of locking and resource grabbing should be handed over to the server for processing to reduce the pressure on the mobile client. 8. What is inter-thread communication? 
In one process, threads do not exist in isolation, and communication between multiple threads is required frequently. 
 
Inter-thread Communication 
One thread transmits data to another thread 
After a specific task is executed in one thread, go to another thread to continue the task. 
 
Common Methods for inter-thread Communication 
-(Void) specified mselecw.mainthread :( SEL) aSelector withObject :( id) arg waitUntilDone :( BOOL) wait; 
-(Void) implements mselector :( SEL) aSelector onThread :( NSThread *) thr withObject :( id) arg waitUntilDone :( BOOL) wait; 
 
Master haomeng is devoted to his contribution and respects the author's Labor achievements. Do not repost them.
 
If the article is helpful to you, you are welcome to donate to the author, support haomeng master, the amount of donation is free, focusing on your mind ^_^
 
I want to donate: Click to donate
 
Cocos2d-X source code download: point I send
 
Game official download: http://dwz.cn/RwTjl
 
Game video preview: http://dwz.cn/RzHHd
 
Game Development blog: http://dwz.cn/RzJzI
 
Game source code transfer: Http://dwz.cn/Nret1
 
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.