Talk C chestnuts together (108th back: C language instance -- thread creation and end 1)

Source: Internet
Author: User
Tags overview example

Talk C chestnuts together (108th back: C language instance -- thread creation and end 1)

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

Here, we will introduce how to create and end a thread. A thread is usually created along with the creation of a process. Because the thread depends on the process, after the process is created successfully, a thread is created to complete some work. For example, the firefox process we introduced in the previous step creates many threads at startup.

The end of a thread is usually controlled by the process, and the process ends the thread according to the needs of the program. However, when the exit function is used to end a process, the thread created by the process will end with the process.

In this case, you may feel abstract. Next we will illustrate how to create and end a thread through a specific example. Before introduction, we will introduce three thread-related functions:

1. pthread_create Function
int pthread_create(pthread_t *thread, const pthread_attr_t *attr,                          void *(*start_routine) (void *), void *arg);

This function is used to create threads. This function has four parameters. Next we will introduce them separately:

The first parameter is a pointer of the pthread type. After the function runs successfully, the Pointer Points to the identifier of the newly created thread. We can use the identifier of the Operation thread to operate the thread; the second parameter is a pointer of the pthread_attr_t type. The Pointer Points to a variable containing thread attributes, which are assigned to the newly created thread. If we do not want to modify the attributes of a thread, we can use a null pointer. The third parameter is a function pointer. the pointer to a function has a parameter whose type is void, the function returns a void pointer. This function is executed after the new thread is created successfully. The fourth parameter is a void pointer, and the variable pointed to by this pointer is treated as the parameter of the function in the third parameter; if the function runs successfully, zero is returned. Otherwise, the corresponding error code is returned. 2. pthread_exit Function
void pthread_exit(void *retval);

This function is used to end a thread. This function has only one parameter.

The parameter is a void pointer. The Pointer Points to a variable that contains content at the end of a thread. It is usually used to store the end state of a thread. If the function runs successfully, zero is returned. Otherwise, the corresponding error code is returned. 3. pthread_join Function
int pthread_join(pthread_t thread, void **retval);

This function is used to wait for the thread to end and is usually used for simple synchronization. Its function is similar to the wait function in the process;

The first parameter is a variable of the pthread_t type, which contains the thread identifier. We can use the first parameter of the pthread_create function to obtain the value of this variable. The second parameter is a second-level pointer, it points to the parameter of the pthread_exit function. Therefore, we usually use this pointer to obtain the State at the end of the thread. If the function runs successfully, zero is returned. Otherwise, the corresponding error code is returned.

When using these functions, you must include the "pthread. h" header file. In addition, the position of the file in the Mint we use is as follows:

/Usr/include/pthread. h

You can check the content. For example, the prototype of the pthread_create function in this file is as follows:

extern int pthread_create (pthread_t *__restrict __newthread,                           const pthread_attr_t *__restrict __attr,                           void *(*__start_routine) (void *),                           void *__restrict __arg) __THROWNL __nonnull ((1, 3));

This function looks very complicated. The others are the same as what we just introduced, but there is a small tail at the end of the letter.

Let's talk about the example of thread creation and termination. 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.