preface

In daily development, the use of multithreading can help us solve a lot of problems, such as the operation of a large amount of data, the execution of complex programs, and the use of locks to achieve some requirements, this series of articles mainly introduces the use of iOS multithreading technology.

  • Multithreading Multiple threads can be started in a process, and each thread can perform different tasks in parallel to make full use of resources and improve computing efficiency.

  • The principle of multi-threading at the same time, the CPU can only handle 1 thread, only 1 thread in the execution, multi-threading concurrent execution, in fact, is the CPU quickly between multiple threads scheduling (switch), if the CPU scheduling thread time is fast enough, it caused the illusion of multi-threading concurrent execution, It’s not that the CPU is performing multiple tasks at the same time.

  • Advantages can properly improve the execution efficiency of the program; Improves resource usage (CPU and memory usage).

  • Disadvantages starting threads occupies a certain amount of memory space (by default, the main thread occupies 1 MB and the sub-threads occupy 512KB). If you start a large number of threads, the memory space will be occupied and the program performance will be degraded. The more threads there are, the more overhead the CPU has on scheduling threads.

In iOS, there are four main multithreading solutions:

IOS: Multithreading (2) — NSThread

In this article, we introduce pThreads

Pthread profile

Pthread is a set of universal multithreading API, which can be used cross-platform in Unix, Linux, Windows and other systems. It is written in C language and requires programmers to manage the life cycle of threads themselves, making it difficult to use.

Pthread is simple to use

    NSLog(@"%s : %@", __func__, [NSThread currentThread]); / * * * * * * * * * * * * * * * * * * * * * * * * * * create a thread * * * * * * * * * * * * * * * * * * * * * * * * * / / / pthread pthread_t thread = NULL; NSString *paramStr = @"paramStr"; // The thread is created successfully. Int res = pthread_create(&thread, // thread object, pass address NULL, // thread attribute, Set to null pthreadMethod, // pointer to function (__bridge void *)(paramStr)); // The argument that the function needs to accept can be nullif (res == 0) {
        NSLog(@"Thread created successfully! pthread begin"); } // Set the state of the child thread to detached, so that the thread will automatically release all resources when it finishes running. Or add pthread_detach(pthread_self()) to the child thread, where pthread_self() is the id of the thread itself pthread_detach(thread); NSLog(@"pthread end"); Void *pthreadMethod(void *parma) {for (NSInteger i = 0; i < 5; i++) {
        NSLog(@"%ld --> %s : %@ param: %@", i, __func__, [NSThread currentThread], (__bridge NSString *)(parma)); [NSThread sleepForTimeInterval: 1.0]; // Simulation time}return NULL;
}

Copy the code

Pthread_create () is the method to create a thread. Just fill in four parameters to create a thread. Let’s look at the output

pthreadMethod()

Pthread Other methods and parameters

Pthread_t: thread ID

Pthread_attr_t: Thread attribute

Pthread_create () : Creates a thread

Pthread_exit () : Terminates the current thread

Pthread_cancel () : Interrupts another thread

Pthread_join () : blocks the current thread until another thread finishes running

Pthread_attr_init () : Initializes thread properties

Pthread_attr_setdetachstate () : Sets the detachstate property

Pthread_attr_getdetachstate () : Gets the attribute of the out-of-state

Pthread_attr_destroy () : Deletes the attributes of a thread

Pthread_kill () : sends a signal to the thread

Pthread_equal (): Compares the thread ids of two threads

Pthread_detach (): Detach the thread

Pthread_self (): Queries the thread id of the thread itself

The above is about pthread and its simple usage. Please refer to relevant demo

Github.com/G-Jayson/Mu…