Pthread Foundation
Pthread is POSIX thread shorthand, a set of common multi-threaded API, for UNIX, Linux, Windows and other systems, cross-platform, portable, use difficult, C language framework, thread life cycle by the programmer management, Baidu Encyclopedia has a detailed introduction, to see, Since iOS development is almost impossible to use, the following is a simple use of pthread to open a sub-thread to handle time-consuming operations
pthread Creating a child thread step
1. import Header File
#import <pthread.h>
2.pthread Creating child threads
- (void) viewdidload {[Super viewdidload]; /** Description: 1. In C, there is no concept of object, the object type ends with-t/ref; The ID of void * and OC in the 2.c language is equivalent; 3. In mixed development, if data is passed between C and OC, bridging is required using __bridge, the purpose of the bridge is to tell the compiler how to manage memory, and no bridging is required in MRC; 4. In OC, in the case of ARC development, the compiler will automatically add retain/release/autorelease at compile time, depending on the code structure. However, ARC is only responsible for managing the memory management of the OC section, not the memory management of the C language code. Therefore, in the development process, if the use of the C language framework appears retain/create/copy/new and other words of the function, most of them need to release, otherwise there will be a memory leak*/ //1. Create a Thread objectpthread_t thread; //2. Create a threadNSString *param =@"Parameters"; intresult = Pthread_create (&thread, NULL, task, (__bridgevoid*) (param)); Result==0? NSLog (@"Success"): NSLog (@"Failure"); //3. Set the status of the sub-thread to detached, the thread will automatically release all resources after it finishes running, or add Pthread_detach (Pthread_self ()) to the child thread, where pthread_self () is to get the ID of the thread itself
Pthread_detach (thread); /** Pthread_create (< #pthread_t _nullable *restrict _nonnull#>, < #const pthread_attr_t *restrict _nullable#& gt;, < #void * _nullable (* _nonnull) (void * _nullable) #>, < #void *restrict _nullable#>) Parameters: 1. Pointing to the thread identifier pointers, which are usually _t/ref at the end of a type in C, and do not need to be used *; 2. Used to set thread properties; 3. A pointer to a function, passing in the function name (the address of the function), the function/task to be executed by the thread; 4. Parameters of the running function; */}/** void * (*) (void *) return value function name function parameter*/void*task (void*param) { //take time-consuming actions hereNSLog (@"new Thread:%@ parameter is:%@", [Nsthread CurrentThread], (__bridge NSString *) (param)); returnNULL;}
Other related functionspthread_t: Thread idpthread_attr_t: Thread Properties Pthread_create (): Creating a Thread Pthread_exit (): Terminating current thread pthread_cancel () : Interrupts the running of another thread Pthread_join (): Blocks the current thread until another thread runs the End Pthread_attr_init (): Initializes the thread's properties Pthread_attr_setdetachstate () : Sets the attribute of the out state (determines whether the thread can be combined at termination) pthread_attr_getdetachstate (): Gets the property of the detached state Pthread_attr_destroy (): Removes the thread's properties Pthread_kill (): Sends a signal to the thread pthread_equal (): Compares the thread identification number of two threads Pthread_detach (): Detach Thread pthread_self (): Query thread's own thread ID number
iOS development-The pthread of multithreaded implementations