The sample code _ thread

 

Thread

 


 

1. Automatic thread separation, commonly used methods:

1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <pthread.h>
 4  
5 pthread_mutex_t mutex;   // Let's first define a "lock"! 
. 6  
. 7  
. 8  void * Fun ( void * Arg) {
 . 9  
10      the pthread_mutex_lock (& the mutex);         // locked: mutual exclusion, resource conservation 
. 11      the printf ( " : Fun Child. 1 \ n-! " ;)
 12 is      SLEEP ( . 1 );
 13 is      the printf ( " Fun: Child 2 \ n-! " );
 14      SLEEP (. 1 );
 15      the printf ( " Fun: Child. 3 \ n-! " );
 16      SLEEP ( . 1 );
 . 17      pthread_mutex_unlock (& the mutex);   // unlock: release 
18 is  
. 19      the pthread_exit (NULL);              // end of the thread head 
20 is  
21 is  }
 22  
23 is  
24  int main ( void )
 25  {
 26 is      
27      the pthread_mutex_init (& the mutex, NULL);         // initial mutex 
28      
29      pthread_t TID; // thread ID 
30     
31 is      IF (! Pthread_create (& TID, NULL, Fun, NULL) = 0    ) {    // create a thread 
32          perror ( " pthread_creat failed " );
 33 is          Exit ( . 1 );
 34 is      }
 35  
36      the pthread_detach (TID);                                 //   separated directly automatic corpses 
37 [  
38 is  
39      the pthread_mutex_lock (& the mutex);         // locked: mutual exclusion, resource conservation 
40      the printf ( " main:! parent. 1 \ n- " );
 41 is      SLEEP ( . 1 );
 42 is     the printf ( " ! main: parent 2 \ n- " );
 43 is      SLEEP ( . 1 );
 44 is      the printf ( " main: parent. 3 \ n-! " );
 45      SLEEP ( . 1 );
 46 is      pthread_mutex_unlock (& the mutex);   // unlock: release 
47  
48      sLEEP ( 5 ); // sleep for five seconds, or the end of the main process, sub-thread jj, no phenomenon! ! 
49     the printf ( "Resource mutually exclusive lock success \ n-!");
 50      return  0 ;
 51 is }

 

 


 

2. Thread the manual separation, to understand:

. 1  void * Fun ( void * Arg) {
 2      the printf ( " Fun: Child \ n-! " );
 . 3      SLEEP ( . 1 );
 . 4      the printf ( " Fun:! Child \ n- " );
 . 5      SLEEP ( . 1 );
 . 6  
. 7      the printf ( " first end of the thread, then corpses \ n- " );
 . 8      the pthread_exit (NULL);       // end head thread 
. 9  
10  }
 . 11  
12 is  
13 is  int main ( void )
 14 {
15     pthread_t tid; // 线程 id
16     
17     if( pthread_create(&tid, NULL, fun, NULL) !=0   ){   // 创建线程
18         perror("pthread_creat failed");
19         exit(1);
20     }
21 
22     printf("main : parent !\n");
23     sleep(10);
24 //    printf("main : parent !\n");
25 //    sleep(1);
26 
27 
28      IF (pthread_join (TID, NULL)! = 0 ) {             // to the sub-thread corpses!   
29          perror ( " pthread_join failed The " );             // The second parameter is the return value of the child thread, custom! 
30          Exit ( . 1 );
 31 is      }
 32      the printf ( " corpses success \ n- " );
 33 is  
34 is  
35      return  0 ;
 36 }

 

 

 

test:


 

 

 

success !

 

 

 

<Notes>


 

1. 

 

Guess you like

Origin www.cnblogs.com/panda-w/p/11059617.html