In multi-CPU multi-threading programming, through the author's learning found that the use of pthreads more and more widely, it is the thread of the POSIX standard, defines the creation and operation of the thread of a full set of APIs. Environment configuration See the previous blog post, configure the environment only need to add #include <pthread.h>, you can use the Pthreads API. This article describes how to use Pthreads to create multiple threads and terminate threads. It is divided into three parts, the first part gives the code example, the second part explains the code, the third part gives the result of the operation.
One, code example
This program creates 5 threads, each of which outputs the Hello world! and the thread number.
Second, the code explanation
Exit Current thread
Defining variables of type pthreads_t
Pthread_create () creates threads and associates them with the task to be performed, allowing the thread to execute the associated task immediately. The first parameter represents the created thread, the second parameter of the thread's Property object (null means default), the third parameter represents the task being performed, this is the previously requested sub-function Printhello, and the fourth represents the parameter that needs to be passed.
Third, the results show