Multi-Threading and network development for iOS multi-threaded Nsthread

Source: Internet
Author: User

Shimen the main contribution, respect the work of the author, please do not reprint.

If the article is helpful to you, you are welcome to donate to the author, support the Shimen, donate any amount, ^_^

I want to donate: Click Donate

Cocos2d-x source code Download: Dot I teleport

Games 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


Basic use of A.nsthread1. Creating and Starting ThreadsA Nsthread object represents a thread

Creating, starting Threads
Nsthread *thread = [[Nsthread alloc] initwithtarget:self selector: @selector (run) Object:nil];
[Thread start];
A thread starts. You run the self's Run method in thread threads

Main thread related usage
+ (Nsthread *) Mainthread; Get the main thread
-(BOOL) Ismainthread; is the main thread
+ (BOOL) Ismainthread; is the main
thread
 2. Other methodsGet current thread
Nsthread *current = [Nsthread CurrentThread];

Scheduling Priority for Threads
+ (double) threadpriority;
+ (BOOL) SetThreadPriority: (double) p;
-(double) threadpriority;
-(BOOL) SetThreadPriority: (double) p;
The scheduling priority value range is 0.0 ~ 1.0, the default 0.5, the higher the value, the higher the priority

The name of the thread
-(void) SetName: (NSString *) n;
-(NSString *) name;
 3. Other ways to create threadsinitiate thread yourself after creating a thread
[Nsthread detachnewthreadselector: @selector (Run) totarget:self Withobject:nil];

Implicitly creating and starting a thread
[Self Performselectorinbackground: @selector (Run) Withobject:nil];

Pros and cons of the above 2 ways to create threads
Advantages: Simple and fast
Cons: Unable to make more specific settings for threads
 4. Thread Life cycle 5. Control thread TurntableStart thread
-(void) start;
Go to Ready status, run status. When the thread task finishes running. Self-initiated into the state of death

Blocking (pausing) a thread
+ (void) Sleepuntildate: (NSDate *) date;
+ (void) Sleepfortimeinterval: (Nstimeinterval) ti;
into a blockage state.

Force Stop Thread
+ (void) exit;
Into the state of death

Note: Once the thread has stopped (died). You can't start the task again
.
 6. Multi-threaded security risksResource sharing
1 blocks of resources may be shared by multiple threads. That is, multiple threads may access the same resource
For example, multiple threads visit the same object, the same variable, the same file

When multiple threads access the same piece of resources, it is easy to raise data confusion and data security issues
 7. Mutual exclusion Lock (in order to prevent multi-threaded resource looting)(1) mutually exclusive lock use format
@synchronized (Lock Object) {//code required to be locked}
Note: Locking 1 parts of code with only 1 locks is not valid with multiple locks

Advantages and disadvantages of mutually exclusive locks
Advantage: It can effectively prevent the data security problem caused by multi-thread snatch resource
Cons: Need to consume a lot of CPU resources

Mutual exclusion Lock Use premise: Multiple threads rob the same piece of resources

Related Professional terminology: Thread synchronization
Thread synchronization means that multiple threads run the task sequentially
Mutually exclusive lock. Is the use of thread synchronization technology
 (2) Locking @synchronized@property (nonatomic, strong) NSObject *lock;Self.lock = [[NSObject alloc] init]@synchronized (self.lock) {...}Note: To use the same "lock", the ability to properly lock code resources Advantage: Effectively prevent multi-thread snatch resourcesCons: Consumes a lot of CPU resources in fact, the member property modifier Atomic is locked, which is the defaultso we generally use nonatomic. Improve Performance try to avoid multi-thread snatch resourcesDispose of the looting resources as much as possible to the server side. (3) about atomic and nonatomic modifiersOC has nonatomic and atomic two choices when defining attributes
Atomic: Atomic attribute, locking for setter method (default is Atomic)
Nonatomic: Non-atomic attribute, does not lock the setter method

Atomic Plus Lock principle
@property (assign, atomic) int age;
-(void) Setage: (int) Age
{
@synchronized (self) {
_age = age;
}
}
 (4) Atomic vs NonatomicNonatomic and Atomic Control
Atomic: Thread safety. Need to consume a lot of resources
Nonatomic: Non-thread-safe, suitable for small memory mobile devices

Recommendations for iOS development
All properties are declared as Nonatomic
Try to avoid multiple threads robbing the same piece of resources
As far as possible to locking, resource-robbing business logic to server-side processing, reduce the pressure of mobile client
 8. Inter-thread communicationWhat is called inter-thread communication
In 1 of processes. Threads often do not exist in isolation, and frequent communication is required between multiple threads

The embodiment of inter-thread communication
1 Threads pass data to another 1 threads
After you run a specific task in 1 threads, go to another 1 thread to continue running the task

Frequent usage of inter-thread communication
-(void) Performselectoronmainthread: (SEL) Aselector withobject: (ID) arg waituntildone: (BOOL) wait;
-(void) Performselector: (SEL) aselector onthread: (Nsthread *) THR Withobject: (ID) arg waituntildone: (BOOL) wait;

Shimen the main contribution, respect the work of the author, please do not reprint.

If the article is helpful to you, you are welcome to donate to the author, support the Shimen, donate any amount, ^_^

I want to donate: Click Donate

Cocos2d-x source code Download: Dot I teleport

Games 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

Multi-Threading and network development for iOS multi-threaded Nsthread

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.