Feeling and hair (can directly ignore ~): Every time to use the thread, should be on the Internet to re-learn the foundation, the example is quite a lot: a good typesetting, not all the talk is not what they want; one is the typesetting is not good, directly skipped. Both have to struggle to find, so it is their own summary, I think every programmer has got a kind of look at others not pleasing to the disease, haha. I hope you can criticize correct, I this typesetting and summary have what to optimize, absolute best effort.
This paper mainly introduces the basic application of Linux thread, and enumerates the usages and examples of several common functions. Header file pthread.h compile option requires adding-pthread common function thread to create function prototypes:
1 int Const void * (*start_routine) (voidvoid *arg);
Parameter description: Thread: Thread idattr: threading attribute, usually set to null (with other attributes, supplemented) Start_route (): Thread entry function arg: Thread entry function Parameter example:
1#include <stdio.h>2#include <unistd.h>3#include <pthread.h>4 5 voidThread_function (void*param)6 {7printf"This is a thread.\n");8 }9 intThread_test (void)Ten { One pthread_t thread_id; A intret; -ret = Pthread_create (&thread_id, NULL, (void*) &thread_function, NULL); - if(Ret! =0) { theprintf"pthread_create fail\n"); - return-1; - } - + /*The main purpose is to see the thread printing*/ -Sleep1); + A } at intMainintargcChar*argv[]) - { - thread_test (); - return 0; -}
Thread Exit Function prototype:
1 void pthread_exit (void *retval);
Note: The active exit for a thread is basically the same as the return function, but Pthread_cleanup_push () and Pthread_cleanup_pop () do not receive return value. To terminate another thread in the threads, you need to use Pthread_cancel (); parameter description: retval: The return value at the end of the thread, which can be obtained by other functions, such as pthread_join (). Wait for the thread function prototype:
1 int void **retval);
Description: The current thread will be suspended while waiting for the thread to exit. Parameter description: Thread: Waiting for the thread's Idretval: Example of the return value of the waiting thread:
1#include <stdio.h>2#include <unistd.h>3#include <pthread.h>4 pthread_t thread1_id, thread2_id;5 voidThread_function1 (void*param)6 {7printf"This is a thread 2.\n");8 while(1) {9printf"thread 1 is running.\n");TenSleep1); One } Aprintf"Thread 1 exit.\n"); - } - voidThread_function2 (void*param) the { -printf"This is a thread 2.\n"); -Sleep2); -printf"thread 2 Cancel thread 1.\n"); + Pthread_cancel (thread1_id); - } + intThread_test (void) A { at intret; -ret = Pthread_create (&thread1_id, NULL, (void*) &Thread_function1, NULL); - if(Ret! =0) { -printf"pthread_create fail\n"); - return-1; - } inret = Pthread_create (&thread1_id, NULL, (void*) &Thread_function2, NULL); - if(Ret! =0) { toprintf"pthread_create fail\n"); + return-1; - } theRET =Pthread_join (thread1_id, NULL); * if(Ret! =0) { $printf"Pthread_join fail\n");Panax Notoginseng return-1; - } the } + intMainintargcChar*argv[]) A { the thread_test (); + return 0; -}To cancel a thread function prototype:
1 int pthread_cancel (pthread_t thread);
Description: Terminates another process in the thread. A thread can set the state to decide whether it can be canceled by another thread (pthread_setcancelstate), which can be canceled by default. Parameter description: Thread: The ID example of the thread to be canceled:
1#include <stdio.h>2#include <unistd.h>3#include <pthread.h>4 pthread_t thread1_id, thread2_id;5 voidThread_function1 (void*param)6 {7printf"This is a thread 2.\n");8 while(1) {9printf"thread 1 is running.\n");TenSleep1); One } Aprintf"Thread 1 exit.\n"); - } - voidThread_function2 (void*param) the { -printf"This is a thread 2.\n"); -Sleep2); -printf"thread 2 Cancel thread 1.\n"); + Pthread_cancel (thread1_id); - } + intThread_test (void) A { at intret; -ret = Pthread_create (&thread1_id, NULL, (void*) &Thread_function1, NULL); - if(Ret! =0) { -printf"pthread_create fail\n"); - return-1; - } inret = Pthread_create (&thread1_id, NULL, (void*) &Thread_function2, NULL); - if(Ret! =0) { toprintf"pthread_create fail\n"); + return-1; - } theRET =Pthread_join (thread1_id, NULL); * if(Ret! =0) { $printf"Pthread_join fail\n");Panax Notoginseng return-1; - } the } + intMainintargcChar*argv[]) A { the thread_test (); + return 0; -}
Just a brief introduction of the basic operation of the thread, more advanced applications later update please ~ ~
Linux Threads-Basic applications