Learn pthreads, multithreading creation and termination

Source: Internet
Author: User

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

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.