----Multithreading technology for advanced programming in the UNIX Environment (2)

Source: Internet
Author: User
Tags exit in function prototype

Creating a thread is essentially determining the entry point to invoke the thread function, which is typically used in pthread_create. After the thread has been created, the associated threading function begins to run. At the end of the function run, the thread will also exit. This is one way to exit the thread, and the other way is to call the Pthread_exit () function interface, which is the active behavior of the End function. It is important to note that when using the thread function, do not call the exit () function easily, because this will cause the whole process to exit, often a process contains multiple threads, so after the call to exit (), all the threads in the process will be ended. Therefore, in the thread, the pthread_exit is used instead of the exit in the process.

Because the data segments in a process are shared, the resources that exit the thread will not be freed as the thread ends after the threads are usually exited. Just as the wait () function can be called between processes to synchronize the middle finger and release resources, there is a similar mechanism between threads, which is the Pthread_join function. Pthread_join can suspend the current thread and wait for the end of the thread, which is a blocking function. The function that calls it waits until the thread that is waiting ends, and when the function returns, the resources that are waiting for the function are freed.

1. Brief description of function syntax.

Pthread_create

Header file: pthread.h

function prototypes: int pthread_create (pthread_t* thread, pthread_attr_t* attr,

void* (Start_routine) (void*), void* Arg);

function passed in value: Thread: Threading identifier

attr: Thread property setting

Start_routine: Thread function entry

ARG: Arguments passed to a thread function

Return value: 0: Success

-1: Failure

Pthread_exit

Header file: pthread.h

Function prototype: void Pthread_exit (void* retval);

The function passes in the value: Retval:pthread_exit () The return value of the caller's thread, but other functions such as Pthread_join retrieve the fetch.

Phread_join

Header file: pthread.h

Function prototype: int pthread_join (pthread_t* thread, void** thread_return);

function passed in value: thread: The identifier of the waiting thread.

Thread_return: User-defined pointer to store the return value of the waiting thread (not a null value);

function return value: Success: 0

Failed:-1


#include <stdlib.h> #include <stdio.h> #include <pthread.h> #include <errno.h> St    atic void* pthread_func_1 (void*);        Static void* pthread_func_2 (void*);      int main (int argc, char** argv) {pthread_t pt_1 = 0;      pthread_t pt_2 = 0;           int ret = 0;      ret = pthread_create (&pt_1, NULL, pthread_func_1, NULL);      if (ret! = 0) {perror ("pthread_1_create");      } ret = pthread_create (&pt_2, NULL, pthread_func_2, NULL);       if (ret! = 0) {perror ("pthread_2_create");       } pthread_join (Pt_1, NULL);            Pthread_join (pt_2, NULL);    return 0;              } static void* Pthread_func_1 (void* data) {int i = 0;              for (; i < 6; i++) {printf ("pthread1!\n");         if (i = = 2) {pthread_exit (0);       } sleep (1);  } return NULL; } Static void* Pthread_func_2 (void* data) {int i = 0;       for (; i < 3; i++) {printf ("pthread2!\n");         } pthread_exit (0);  return NULL;     }

Running result: g++ linux1.cpp-o pthread-lpthread

[Email protected] linux_chen]$./pthread
This is pthread1!
This is pthread2!
This is pthread2!
This is pthread2!
This is pthread1!
This is pthread1!





Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

----Multithreading technology for advanced programming in the UNIX Environment (2)

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.