Talk C chestnuts together (110th back: C language instance-thread identifier)

Source: Internet
Author: User

Talk C chestnuts together (110th back: C language instance-thread identifier)

Hello, everyone. In the previous session, we talked about the example of thread creation and termination. This example is as follows:Thread ID. When you leave the rest of your time, your words will go right. Let's talk C chestnuts together!

We will introduce how to create a thread with a thread identifier in the previous article. I will explain it again today.

We can use the command: ps x to view the process and its threads, but the command running result is not displayedThread ID). The thread identifier is valid only in the process where it is created. Therefore, it is not necessary to know the thread identifier in most cases. Of course, if you need to view the thread identifier, we can use the system function to obtain the thread identifier.

pthread_t pthread_self(void);
This function is used to obtain the thread identifier, similar to the function for obtaining the process identifier: getpid (). This function can be directly used without parameters. The thread identifier is returned when the function runs successfully, this function will not fail to run;

In Linux, to improve code portability,The unsigned int is usually used as the type of the thread identifier.. We can print the Identifier value for you to view.

Pthread_t thread_id; thread_id = pthread_self (); // obtain The thread identifier printf ("The ID of thread is: % u \ n", (unsigned int) thread_id ); // print the thread identifier

In addition, we can also get the thread Identifier from the parameter of the pthread_create function when creating a thread.

Res = pthread_create (& thread_value, NULL, thread_func, (void *) param); if (0! = Res) {printf ("thread can't be created \ n"); return 1;} printf ("The ID of thread is: % u \ n ", (unsigned int) thread_value); // print the thread identifier

The readers will not write the code in the text, and the detailed code will be put into my resources. You can download and use it.

This Code was modified on the basis of the previous Code. The Code mainly adds the code for obtaining and displaying the thread identifier. In addition, when compiling the code, you need to use the l parameter to import the Link Library, otherwise there will be a link error. For example:

Gcc ThreadID. c-o s-lpthread // use gcc to compile the ThreadID. c file and generate an executable file named s.

The following is the running result of the program. For details, refer:

Create a thread The process ID is 3477 // display the process IDThe ID of The thread being created is: 3075689280 // obtain the thread ID when creating The thread, and print this is the function, it is running normally. and the param is: Thread function param The ID of thread is: 3075689280 // obtain the Thread ID when The thread is running, and print The old status is 0 thread function running finished and the status is: 3

From the preceding running results, we can see that the thread ID we obtained during thread creation is exactly the same as the thread ID obtained during thread running.The thread IDs obtained twice are: 3075689280.

Let's talk about the example of thread identifiers. I want to know what examples will be provided later, and I will try again.

Related Article

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.