Basic usage of POSIX thread I want to use the thread, but I have never understood the thread very well. I have read some documents and made two examples and summarized them. If not, please correct me. 1. Basic functions Pthread_create, pthread_detach, pthread_join, pthread_exit, pthread_self For specific meanings and parameters, see man or book. Other functions will not be used yet. 2. Basic usage Program1 Program function: Main generates a thread. The thread generates several random numbers of 60-based on the parameters transmitted by main. main exits after the thread exits. 1 # include <stdlib. h> 2 # include <stdio. h> 3 # include <pthread. h> 4 # include <time. h> 5 6 int myrand (void * CNT) 7 { 8 int min = 60; 9 int max = 100; 10 int randcnt = * (int *) CNT ); 11Int I = 0; 12 pthread_t thread_id = pthread_self (); 13 14/* init the Random Seed */ 15 srand (unsigned INT) Time (null )); 16For (; I <randcnt; I ++ ){ 17/* create random number in [60,100 )*/ 18 printf ("thread_id = % d rand () % 02d = % d" N ", 19 thread_id, I, min + rand () % (max-min )); 20Sleep (1 ); 21} 22 // return 11; 23 pthread_exit (void *) 11 ); 24} 25 26 int main (INT argc, char * argv []) 27 { 28Pthread_t tid; 29 void * result; 30 int reqrandcnt = 5; 31 32 If (pthread_create (& tid, null, (void *) myrand, (void *) & reqrandcnt) = 0 ){ 33Printf ("myrand thread create OK! "N "); 34 // pthread_detach (TID ); 35} 36 IF (pthread_join (TID, & result) = 0 ){ 37Printf ("thread tid = % d, result = % d" N ", tid, (INT) result ); 38} 39 return 0; 40 // pthread_exit (void *) 22 ); 41} One running result: Thread_id = 1082367168 rand () 00 = 95 Myrand thread create OK! Thread_id = 1082367168 rand () 01 = 71 Thread_id = 1082367168 rand () 02 = 63 Thread_id = 1082367168 rand () 03 = 81 Thread_id = 1082367168 rand () 04 = 66 Thread tid = 1082367168, result = 11 Notes 1)Main uses pthread_create to generate a thread. The most important parameter is the third and fourth parameters, the third parameter is the function for completing the thread, and the fourth parameter is the parameter passed to the thread. Here is an integer. If the thread completes the function to generate a random number of X values in [m, n) according to the main requirement, you can define a constructor for the three parameters to pass to the thread. 2) Main uses pthread_join to wait for the thread to exit and then exit, similar to the wait function of the process. It is worth noting that the second parameter of the function points to the return value of the thread and uses a second-level pointer. In addition, pthread_join and pthread_detach can only be used. For more information, see references. <References> Generally, the running of each thread in a process is independent of each other. The termination of the thread will not be notified or affect other threads, the resources occupied by the terminated thread will not be released with the termination of the thread. Just as processes can use wait () system calls to synchronously terminate and release resources, there is a similar mechanism between threads, that is, the pthread_join () function. The caller of pthread_join () will suspend and wait for the termination of the th thread. retval is the return value of the pthread_exit () caller thread (thread ID is th). If thread_return is not null, * thread_return = retval. It should be noted that a thread only allows a unique thread to use pthread_join () to wait for its termination, and the waiting thread should be in the join state, that is, non-detached state. If a thread in the process executes pthread_detach (th), the th thread is in the detached state, which enables the th thread to independently release the occupied memory resources when it stops running, pthread_join () cannot be synchronized at the same time. After pthread_detach () is executed, an error will be returned for the th request pthread_join (). The memory occupied by a join thread is released only when a thread executes pthread_join (). Therefore, to avoid Memory leakage, all threads terminate, either it has been set to detached or pthread_join () needs to be used for collection. 3) whether the main thread uses pthread_exit or return Using pthread_exit will only cause the main thread to exit, and the generated sub-thread will continue to execute; using return, all threads will exit. To make sure that the sub-thread can always be fully executed (no exit in the middle), one way is to call pthread_join in the main thread to wait for it, that is, pthread_create/pthread_join/pthread_exit or return; one way is to use pthread_exit when the main thread exits, so that the thread can continue to execute, that is, pthread_create/pthread_detach/pthread_exit; another way is pthread_create/pthread_detach/return, at this time, it is necessary to ensure that the main thread cannot exit, at least before the sub-thread is completed. The third method is used in the current project. The main thread is an endless loop, and some sub-threads are endless loops. <References> Theoretically, pthread_exit () is the same as the exit function of the thread host function. When the function ends, pthread_exit () is automatically called internally to clean up thread-related resources. But in fact, the two are very different because of the compiler processing. Calling pthread_exit () in the main function (main () only causes the thread where the main function is located to exit (which can be said to be the main thread of the process, the compiler causes the calling process to exitCode(For example, _ exit () causes the process and all its threads to stop running. |