Objective-C Programming Summary (Article 10) parallel development and thread management

Source: Internet
Author: User

This article is hard to understand. Because this is a big question, and I just want to summarize it for myself, I didn't want to write a long tutorial. Think about it. Write a memorandum to make it concise.

According to the development guidelines provided by Apple, it is not recommended to use custom threads, but to use block and nsopration for asynchronous calls. Because the internal implementation ensures better resource management. The price reference for thread creation is provided. Mac OS and iOS not only need to allocate stack space for threads, but also kernel space. In this way, threads consume a lot of resources. However, in some cases, some special thread creation methods must be used, suchProgramTo end the previous data persistence operation, you need to create a jointable thread ). In this case, the POSIX thread creation method is used.

Here is the official reference table for thread resource consumption:

 

Item Approximate cost Notes
Kernel data structures Approximately 1 KB This memory is used to store the thread data structures and attributes, much of which is allocated as wired memory and therefore cannot be paged to disk.
stack space 512 KB (secondary threads) 8 MB (Mac OS X main thread) 1 MB (IOS main thread) the minimum allowed stack size for secondary threads is 16 KB and the stack size must be a multiple of 4 kb. the space for this memory is set aside in your process space at thread creation time, but the actual pages associated with that memory are not created until they are needed.
Creation Time Approximately 90 microseconds This value reflects the time between the initial call to create the thread and the time at which the thread's entry point routine began executing. the figures were determined by analyzing the mean and median values generated during thread creation on an Intel-based iMac with a 2 GHz Core Duo processor and 1 GB of Ram running Mac OS X v10.5.

 

Thread creation time, 90 ms. This is later than the creation time of most instances. The kernel data occupies about 1 kb of space, and the minimum stack space is 16 KB, which is generally kb for ios/8 MB for Mac. This data looks a bit harsh. 4 or 5 off-the-shelf iPhone programs require 2 MB of data, while the general available stack space for iPhone is about 100 MB.

However, the size of the stack space can be set during thread creation:

Nsthread-you can call [[nsthread alloc] init…] Setstacksize: int.

POSIX-set pthread_attr_t and use the pthread_attr_setstacksize method.

Multiprocessing services-pass the stacksize parameter through the mpcreatetask.

 

To sum up, the operations related to threads include:

    1. Nsthread
    2. Nsoperation, Which is scheduled through nsoperationqueue.
    3. Use GCD

Nstimer, idle notification, and asynchronous call are all asynchronous call methods. I don't think it is summarized as a thread-related function.

In addition, you need to pay attention to the thread scheduling problem.

The first is the end of the Management thread. Getting notifications at the end of a thread is a waste of polling methods. I currently know at least three methods.

  1. With the help of semaphore. Cocoa, the dispatch semaphore communication mechanism can be used to implement operations such as wait and signal. The main thread waits for a semaphore. At the end of each thread, call signal for semaphore to unlock the waiting state of the main thread.CodeExample:
    Dispatch_semaphore_t SEM = dispatch_semaphore_create (0); nsoperation * OP1 = [[myoperationobj alloc] initwithsema: SEM]; [queue addoperation: OP1]; then (SEM, dispatch_time_forever ); nslog (@ "the operation is ended ");
  2. Use runloop. Runloop is a big topic. For details, refer to here: http://www.cocoachina.com/iphonedev/sdk/2011/1111/3487.html. At least better than the official language of the detour. Basically, you can understand it. Sample Code:
    While (op1.ended) {[[nsunloop currentrunloop] runmode: nsdefaultrunloopmode beforedate: [nsdate distantfuture];} // main method of OP1-(void) Main {While (yes) {.....} // when end [self defined mselecw.mainthread: @ selector (Optional Y) withobject: Nil waituntilend: No]; // The specified mselector can immediately trigger runloop event processing .}

You can use a custom port or nsthread threaddictionary to transmit data about threads.

Then nsoperation. This is a built-in thread execution method. You need to define a class, integrate nsoperation, and then put the instance of this class into nsoperationqueue.

@ Interface myop: nsoperation @ end @ implementation @ myop-(void) Main {// body ...} @ end // main nsoperationqueue * queue = [nsoperationqueue new]; myop * op = [myop new]; [queue addoperation: op]; [queue setmaxconcurrentoperationcount: 2]; // otherwise, [queue release]; [op release] will be executed synchronously in the put order;

 

Finally, block should be supported by GCD.

Define a block:

 
Void (^ bblock) (void) = ^ (void) {// body ;}

 

To reference an object (or variable) in a block, the object (or variable) must be declared as static or modified with _ block.

_ Block int blocklocal = 100; static int staticlocal = 100;

 

Another method can be used to iterate the block, that is, block_apply. It can help you put the loop into multi-core support.

 
NSAID utoreleasepool * Pool = [[NSAID utoreleasepool alloc] init]; initdata (); dispatch_queue_t queue = dispatch_get_global_queue (bytes, 0) ;__ block int sum = 0; __block int * parray = data; // iterations // dispatch_apply (length, queue, ^ (size_t I) {sum + = parray [I];}); nslog (@ "> sum: % d", sum); dispatch_release (Queue); [pool drain];

After the block is defined, put it into the queue for execution.

Dispatch_queue_t queue = dispatch_get_global_queue (dispatch_queue_priority_default, 0); dispatch_async (queue, bblock );

 

Block cannot be modified

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.