Linux multithreading practice (2) and linux multithreading practice
POSIX thread Library
Functions related to threads constitute a complete series. Most functions are named as "pthread _". To use these function libraries, you must introduce the header <pthread. h>, and when linking these thread function libraries, use the "-lpthread" option of the compiler command. [The "-pthread" option must be added to the Ubuntu system, instead of "-lpthread ", for example, Ubuntu 14.04 or deep Ubuntu]
Pthread_create
Create a new thread
int pthread_create(pthread_t *restrict thread, const pthread_attr_t *restrict attr, void *(*start_routine)(void*), void *restrict arg);
Parameters:
Thread: thread ID
Attr: sets the thread attribute. If attr is NULL, the default attribute is used.
Start_routine: The function address. The function to be executed after the thread starts.
Arg: parameter passed to the thread startup Function
Returned value: 0 is returned for success; error code is returned for failure.
Appendix-error check
Traditional UNIX functions: 0 is returned for success,-1 is returned for failure, and the global variable errno is set to specify the error type. However, when the pthreads function fails, the global variable errno is not set (while most other POSIX functions set errno ). Instead, the error code is returned through the return value;
Pthreads also provides the errno variable in the thread to support other code that uses errno. For pthreads function errors, we recommend that you use the return value for determination, because reading the return value is less overhead than reading the errno variable in the thread!
// Practice # include <iostream> # include <errno. h> # include <unistd. h> # include <pthread. h> # include <string. h> # include <stdio. h> # include <stdlib. h> using namespace std; void * thread_routine (void * args) {for (int I = 0; I <20; ++ I) {printf (""); fflush (stdout); usleep (20);} return 0;} int main () {pthread_t tid; int ret = 0; if (ret = pthread_create (& tid, NULL, thread_routine, NULL), NULL )! = 0) {fprintf (stderr, "% s \ n", strerror (ret); exit (-1) ;}for (int I = 0; I <20; ++ I) {printf ("B"); fflush (stdout); usleep (20) ;}cout <endl; pthread_join (tid, NULL ); // This function will encounter: waiting for the end of the thread}
Running result (it can be found that the threads run alternately and the running conditions are different ):
Pthread_exit
Thread termination
void pthread_exit(void *value_ptr);
Parameters:
Value_ptr: Specifies the return value of the thread. Note: value_ptr cannot point to a local variable.
Pthread_join
Wait until the thread ends
int pthread_join(pthread_t thread, void **value_ptr);
Parameters:
Thread: thread ID
Value_ptr: it points to a pointer, which points to the return value of the thread (the user obtains the return value of the thread)
Returned value: 0 is returned for success; error code is returned for failure.
// Practice void * threadFunction (void * args) {cout <"Start Thread... "<endl; cout <" End Thread... "<endl; pthread_exit (NULL);} int main () {pthread_t thread; // start the creation and startup thread pthread_create (& thread, NULL, threadFunction, NULL ); cout <"Main Running... "<endl; cout <" Main Ending... "<endl; // wait for the thread to end pthread_join (thread, NULL); return 0 ;}
Pthread_self
Return thread ID
pthread_t pthread_self(void);DESCRIPTIONThe pthread_self() function shall return the thread ID of the calling thread.
// Practice 1: void * thread_routine (void * args) {cout <pthread_self () <"START... "<endl; for (int I = 0; I <20; ++ I) {printf (" A "); fflush (stdout); usleep (20 );} sleep (3); cout <pthread_self () <"END... "<endl; pthread_exit (const_cast <char *> (" thread_routine exit "); // return (void *)" EDF ";} int main () {pthread_t tid; int ret = 0; if (ret = pthread_create (& tid, NULL, thread_routine, NULL), NULL)! = 0) {fprintf (stderr, "pthread_create: % s \ n", strerror (ret); exit (EXIT_FAILURE) ;}for (int I = 0; I <20; ++ I) {printf ("B"); fflush (stdout); usleep (20) ;}cout <endl; void * threadValue; if (ret = pthread_join (tid, & threadValue ))! = 0) {fprintf (stderr, "pthread_join: % s \ n", strerror (ret); exit (EXIT_FAILURE);} cout <"return message: "<static_cast <char *> (threadValue) <endl ;}
// Practice 2: The Master thread and sub-thread transmit data typedef struct _ Student {char name [20]; unsigned int age;} Student; void * threadFunction (void * args) {cout <"In Thread:" <pthread_self () <endl; Student tmp = * (Student *) (args); cout <"Name: "<tmp. name <endl; cout <"Age:" <tmp. age <endl; pthread_exit (NULL);} int main () {Student student = {"xiaofang", 22}; pthread_t thread; // start the creation and startup thread pthread_create (& thread, NULL, threadFunction, & student); // wait until the thread ends pthread_join (thread, NULL); return 0 ;}
Pthread_cancel
Cancels a thread in execution.
int pthread_cancel(pthread_t thread);
Returned value: 0 is returned for success; error code is returned for failure;
Pthread_detach
Separate a thread-if the main thread does not end and pthread_join is not called at the end of the newly created thread, a stiff thread is generated, this problem can be solved by setting the thread as a detach;
int pthread_detach(pthread_t thread);
Returned value: 0 is returned for success; error code is returned for failure;
Summary: Process VS. Thread
Process (pid_t) |
Thread (pthread_t) |
Fork |
Pthread_create |
Waitpit |
Pthread_join/Pthread_detach |
Kill |
Pthread_cancel |
Pid |
Pthead_self |
Exit/return |
Pthread_exit/return |