Multithreading-Pthread, Nsthread

Source: Internet
Author: User
Tags posix

1. Pthread

Pthread Simple Introduction, Pthread is a set of common multi-threaded API, can be unix/linux/windows and other washes cross-platform use, the use of C language, the programmer needs to manage the life cycle of the thread, the use of greater difficulty, So we almost don't apply pthread in iOS development, here's a quick look.

-Introduction from Baidu Encyclopedia-

POSIX threads, referred to as pthreads, are the POSIX standards of this case city. The standard defines a complete set of APIs for creating and manipulating threads. In Unix-like operating systems (Unix, Linux, Mac OSX), pthreads is used as the operating system thread. The Windows operating system also has its ported version of Pthreads-win32.

How to use 1.1 pthread

A. First to include header files #import <pthread.h>

B. Second to create threads and turn on thread execution tasks

Create thread--Define a pthread_t type variable pthread_t thread;//open Thread--perform task pthread_create (&thread, NULL, run, NULL); void * Run (void * param)    //New thread call method, inside for the task to be performed {    NSLog (@ "%@", [Nsthread CurrentThread]);    return NULL;}

Pthread_create (&thread, null,run,null); Parameters:

A. The first parameter &thread is a thread object

B. The second and fourth are thread properties, which can be assigned a value of NULL

C. The third run represents a pointer to a function (the run corresponding function is a task that needs to be performed in a new thread)

2. Nsthread

Nsthread is officially provided by Apple and is more object-oriented than Pthread, and is easy to use, and can manipulate thread objects directly. However, the programmer is also required to manage the thread's life cycle (primarily created), and we occasionally use nsthread during the development process. For example, we often use [Nsthread CurrentThread] to display current process information.

2.1 Creating and starting a thread

-Create the thread first, then start the thread, this time the line thread executes is a scheduled state, directly controlled by the CPU

Nsthread *thread = [[Nsthread alloc] initwithtarget:self selector: @selector (run) Object:nil]; [Thread start];    When a thread is started, the run method of self is executed in threads thread

-Self-starting thread after creating thread

[Nsthread detachnewthreadselector: @selector (Run) totarget:self Withobject:nil];

-Implicitly create and start threads

[Self Performselectorinbackground: @selector (Run) Withobject:nil];
2.2 Common thread-related methods
Get the main thread + (Nsthread *) Mainthread;    Determines whether the main thread (object method)-(bool) ismainthread;//determines whether it is the main thread (class method) + (bool) Ismainthread;    Get current thread Nsthread *current = [nsthread currentthread];//thread Name--setter Method-(void) SetName: (NSString *) n;    The name of the thread--getter method-(NSString *) name;
2.3 Thread state Control method

-Start Threading method

-(void) start;//thread into ready state, running state. Automatically enters the death state when the thread task is completed

-Blocking (pausing) threading methods

+ (void) Sleepuntildate: (NSDate *) date;+ (void) Sleepfortimeinterval: (nstimeinterval) ti;//thread into blocking state

-Force Stop Thread

+ (void) exit;//thread enters death state
2.4 State Transitions for threads

When we create a new thread

Nsthread *thread = [[Nsthread alloc] initwithtarget:self selector: @selector (run) Object:nil];

The performance in memory is:

When [thread start] is called, the system puts the thread object into a pool of scheduled threads, and the thread object is in the ready state: as shown in:

Of course, there are other thread objects that can be scheduled in the thread pool, as shown in:

Let's take a look at the state transitions of the current thread
    • If the CPU now dispatches the current thread object, the current thread object goes into the running state, and if the CPU dispatches other thread objects, the current thread object returns to the ready state.
    • If the CPU calls the sleep method \ waits for a synchronization lock while running the current thread object, the current thread object is in a blocking state, and when the sleep time \ Gets the synchronization lock, it returns to the ready state.
    • The current thread object enters a dead state if the thread task finishes executing \ exception forced exit when the CPU is running the current thread object.

The state of the specific current thread object changes as shown:

We can see that when we create a thread object and call the Run method, we just turn the state of the thread into a ready state, and the dispatch thread is CPU-controlled.

Multithreading-Pthread, 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.