Oc-19.pthread and Nsthread

Source: Internet
Author: User

Brief introduction
    • Proper use of multithreaded programming can provide task execution efficiency and system resource utilization

      • Multithreading is to improve resource utilization, and application responsiveness, multiple threads share application resources
      • Each application has a main thread, which is usually used to refresh the UI interface, etc.
      • More time-consuming tasks, if placed in the main thread, can clog the main thread, fail to respond to user actions, and typically create their own threads for time-consuming tasks and execute concurrently with the main thread
    • Multithreaded programming improves the utilization of system resources and the task processing speed to some extent, but the thread is not easy to be too much, otherwise it will cause the following problems.

      • Too many threads can cause frequent scheduling of processors, and thread scheduling consumes a lot of system resources.
      • Multiple threads in the same process share the resources of the process, and multiple threads accessing the resource need to be synchronized, and if too many threads increase the difficulty of synchronization, or even fail to implement.
How to implement multithreading in iOS development
    • Pthread

      • Cross-platform for multiple operating systems with strong portability
      • is a set of pure C language Common API, and the life cycle of the thread needs the programmer to manage, use more difficult, usually do not use
    • Nsthread

      • The OC language-based API makes it easy to use, object-oriented operations
      • The thread's declaration cycle is managed by programmers and is occasionally used
    • GCD

      • C-based API to take full advantage of device multicore, designed to replace threading technology such as Nsthread
      • The life cycle of a thread, automatically managed by the system, often used
    • Nsoperation

      • Based on the OC language API, the bottom layer is GCD, which adds some easier-to-use features, using more object-oriented
      • The thread life cycle is automatically managed by the system and is often used
Pthread
    • Creating Threads

      • Define Thread variables:

        • pthread_t thread
      • Initialize thread variables:

        • Pthread_create (&thread, NULL, run, NULL)
    • Sets/Gets the number of concurrent executions of a thread

      • To set the number of concurrent executions:

        • int pthread_setconcurrency (int)
      • Gets the number of concurrent executions of the thread:

        • int pthread_getconcurrency (void)
    • Perform a single task (typically used to design a singleton mode)

      • Define Tags:

        • pthread_once_t Oncetoken
      • Perform one-time tasks:

        • int Pthread_once (pthread_once_t *, void (*) (void))
    • Convert thread state to unjoinable state to ensure the release of resources

      • int Pthread_detach (pthread_t)
    • Exit thread

      • void Pthread_exit (void *)
Nsthread
    • Thread life cycle operations

      • Methods for creating threads

        • -(Instancetype) Initwithtarget: (ID) Target selector: (SEL) se Lector object: (ID) argument, the thread created by this method needs to manually start
        • + (void) Detachnewthreadselector: (SEL) selector totarget: (ID) Target withobject: (ID) argument, automatically starts after the thread is created
        • -(void) Performselectorinbackground: (SEL) aselector Withobject: ( ID) arg, create the thread implicitly and start
      • Configure thread

        • set thread name by Name property
        • + (BOOL) Setthreadpriorit Y: (double) p sets the priority of the thread
      • Start thread

        • -(void) Start
      • Block thread

        • + (void) Sleepuntildate: (NSDate *) Date
        • + (void) Sleepfortimeinterval: (nstimeinterval) Ti
      • Cancel thread

        • -(void) cancel, currently executing thread does not stop immediately
      • Force quit thread

        ul>
      • + (void) exit
    • Other operations for Nsthread

      • Related to the main thread

        • + (Nsthread *) Mainthread, get the main thread
        • + (BOOL) Ismainthread, judging if the current thread is not the main path
      • Related to current thread

        • + (Nsthread *) CurrentThread, gets the current thread
      • Determine the state of a thread

        • \ Determines whether a thread is executing by using the executing property
        • \ Determines whether a thread is finished by using the finished property
        • \ Determines whether a thread is canceled by using the Cancelled property
    • Thread synchronization

      • Cause: Multiple threads accessing the same resource are likely to cause data confusion and data security issues
      • Solution: Use a mutex to resolve the mutex access problem, lock the critical resource (lock) {}, and usually use self as the lock
      • Note: Because thread synchronization consumes a large amount of resources, you should try to avoid multiple threads accessing the same resource, and the logic of thread synchronization is usually referred to the server side for implementation
    • Communication between threads

      • Returning from a child thread to the main thread
        --(void) Performselectoronmainthread: (SEL) Aselector withobject: (ID) arg waituntildone: (BOOL) Wait modes: (Nsarray *) Array
        -array Specifies the mode of Runloop, and if it is empty, aselector is not executed
        -The caller of the method is the caller of the Aselector
        --(void) Performselectoronmainthread: (SEL) Aselector withobject: (ID) arg waituntildone: (BOOL) wait
      • From one thread to another (including the main thread)
        --(void) Performselector: (SEL) aselector onthread: (nsthread ) THR Withobject: (ID) arg waituntildone: (BOOL) wait Modes: (Nsarray ) array
        --(void) Performselector: (SEL) aselector onthread: (Nsthread *) THR Withobject: (ID) arg waituntildone: (BOOL) wait
      • Communication can also be achieved by Nsport objects

Oc-19.pthread and 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.