IOSmultithreaded Programming for beginners, it is always difficult to understand and grasp, and nowthrough a few examples to more systematic and comprehensive understanding of iOS multithreaded programming, we hope to help.
1: First A brief introduction to what is called threading A basic scheduling unit that can execute concurrently, has minimal system resources, and shares process resources. Common heap, own stack (official information indicates that the iOS main thread stack size is 1M, other threads are 512K). Concurrent execution progress is not controllable, the non-atomic operation is prone to inconsistent state, lock control and the risk of deadlock.
Threads in the 2:ios iOS main thread (UI thread), most of our business logic code runs in the main thread. Without special requirements, threading should not be introduced to increase the complexity of the program. Application Scenario: Logic execution time is too long, seriously affect the interactive experience (interface card dead) and so on.
There are three main methods of IOS multithreading (1) Nsthread (2) Nsoperation (3) * *
1. Main Queue: As the name implies, it runs on the main thread and is obtained by Dispatch_get_main_queue. UI related to using the main Queue.
2.Serial Quque (Private dispatch queue) Each time you run a task, you can add multiple, execution order FIFO (queue, FIFO first, and out). Usually refers to a programmer's generation, such as:
- dispatch_queue_t mycustomqueue = Dispatch_queue_create ("Example. Mycustomqueue ", NULL);d Ispatch_async (Mycustomqueue, ^{
- for (int abc=0;abc<100;abc++)
- {
- printf ("1.Do some work here.\n");
- }
- });d Ispatch_async (Mycustomqueue, ^{
- for (int abc=0;abc<100;abc++)
- {
- printf ("2.Do some work here.\n");
- }
- });d ispatch_queue_t myCustomQueue2 = Dispatch_queue_create ("Example. MyCustomQueue2 ", NULL);d Ispatch_async (myCustomQueue2, ^{
- for (int abc=0;abc<100;abc++)
- {
- printf ("1. MyCustomQueue2 do some work here.\n ");
- }
- });d Ispatch_async (myCustomQueue2, ^{
- for (int abc=0;abc<100;abc++)
- {
- printf ("2. MyCustomQueue2 do some work here.\n ");
- }
- });
Copy Code However, Because Mycustomqueue and myCustomQueue2 are in two queues, in the queue mycustomqueue: "1.Do some work here." In "2.Do some work here." Before the Mycustom Queue2 queue: "1. MyCustomQueue2 do some. " In the "2. MyCustomQueue2 do some. " Before. The tasks in Mycustomqueue and myCustomQueue2 are not sequential. And not concurrent. 3. Concurrent Queue (Global dispatch queue): You can run multiple tasks at the same time, and each task starts in the order in which they are joined, depending on the order in which they are terminated. Use Dispatch_ Get_global_queue obtained. The following is a brief introduction to these three methods 1.NSThread Call methods as follows: If the function requires input parameters, it can be passed in from object.
- (1) [Nsthread detachnewthreadselector: @selector (threadinmainmethod:) totarget:self Withobject:nil];
- (2) nsthread* myThread = [[Nsthread alloc] initwithtarget:self selector: @selector (threadinmainmethod:) Object:nil];
- [MyThread start];
- (3) [obj performselectorinbackground: @selector (Threadme) Withobject:nil];
Copy Code Ask a question: If a viewcontroller run a thread,thread is not finished, the Viewcontroller was release, the result?
After the test, thread does not end, Viewcontroller has been retained, will not execute the Dealloc method.
2.NSOperation Nsoperation is also a kind of multithreading, Nsopertaion has 2 kinds of forms
(1) Concurrent execution Concurrent execution you need to overload the following 4 methods
- Execute the task main function, the entry function that the thread runs
- -(void) Start
- Whether to allow concurrency, return yes, allow concurrency, return no not allowed. Default returns NO
- -(BOOL) isconcurrent
- -(BOOL) isexecuting
- Whether it has been completed, this must be overloaded, or put in the nsoperationqueue in the nsopertaion can not be released normally.
- -(BOOL) isfinished
Copy Code For example, Testnsoperation:nsoperaion overloads the above 4 methods,
Declare a nsoperationqueue,
- Nsoperationqueue *queue = [[[[Nsoperationqueue alloc] init] autorelease];
- [Queue addoperation:testnsoperation];
Copy Code It will automatically call the Start function in the testnsoperation, if you need more than one nsoperation, you need to set some properties of the queue, if more than one nsoperation dependencies, you can also set the specific reference to the API documentation.
(2) Non-concurrent execution -(void) Main You just need to overload the main method.
3.**
* * Very strong, I have little experience in using. But SCORPIOZJ summary of the more comprehensive (http://www.cnblogs.com/scorpiozj/archive/2011/07/25/2116459.html)
Meanwhile, this article also introduces the more detailed http://www.cnblogs.com/vinceoniphone/archive/2011/04/07/2007968.html official tutorials
* * is closely linked to block, so it is better to know the next block (see here). * * is a C level function, which means it also provides C function pointers as parameters, convenient for C programmers.
Let's start with the following:
- Dispatch_async (dispatch_queue_t queue, dispatch_block_t block);
Copy Code Async indicates that the block represents the thing you want to do asynchronously, and the queue is the one who handles the task. (in addition to async and Sync,delay, this article takes async as an example).
Multithreading is used in programs because the program often needs to read the data and then update the UI. For a good user experience, the data-read operation tends to run in the background, so as to avoid blocking the main thread. There are three types of queue to handle.
So we can get a general idea of the framework using * *:
- dispatch_queue_t Newthread = dispatch_get_global_queue (dispatch_queue_priority_default, 0); Dispatch_async (newthread,^{[self
- Downloadimage:imageurl]; });
Copy Code Section: Nsthread may be able to switch faster because ARMV6 or later processors provide a very powerful thread switching mechanism. However, Nsthread does not take multi-core allocations, because the system interface first guarantees the reliability of the user thread. The Grand Central Dispatch explicitly uses the dispatch queue to do a multi-core dispatch, so it is faster to use g_c_d if it is on a multicore processor. If your processor is single-core, you can use the switch faster nsthread.
You wake up every morning to fight for the goal, it should be the goal of your life.
Original link: http://www.gsdtarena.com/a/iosjc/616_2.html |