Control and separation of threads
Threads and Processes : A thread can be said to be an execution stream of the system, which is used by the operating system to dispatch the CPU to execute the base
This module, thread it exists in a process that is equivalent to an entity that takes on system resources,
and the thread is used to go executed, it and the process
some of them are shared:
1. File Descriptor Descriptor
2. How each signal is processed (sig_ign, SIG_DFL, or custom signal processing functions)
3. Current working directory
4. User ID and Group ID
some are independent:
1. Stack space
2. Hardware context (including the values of various registers, stack pointers, program counters)
3. Thread ID
4. Signal Shielding Word
Value of 5.errno
6. Scheduling priority
Control of the thread:
The following functions are defined in the POSIX standard for threading control
* * Compile to include Phread in the package later
#include <pthread.h> int pthread_create (pthread_t *thread, const pthread_attr_t *attr, void * (*start_routine) (void *), void *arg);
pthread_t *thread is an output parameter used to return a thread number
Start_routine is a function of a thread, and Arg is a passing value
PTHREAD_ARRT_T is used to set the properties of the thread, we use the system default NULL
The function returns 0 successfully, and the failure returns the error code
int Pthread_join (pthread_t thread, void **retval);
Similar to Waitpid, this function is used to wait for the thread, the output parameter retval the exit code used to output the thread
The function returns 0 successfully, and the failure returns the error code
Three ways to return a thread:
1, return from inside thread;
2, use function Pthread_exit function to return;
3, the process is canceled externally with the Pthread_cancel function;
Here is the test code
1 #include <stdio.h> 2 #include <pthread.h> 3 void *write1 (Void*arg) 4 { 5 int count=15; 6 while (count-->0) 7 { 8 printf ("write thread1\n"); 9 sleep (1); 10 } 11 return (void*) 1; 12 } 13 void *write2 (Void*arg) 14 { 15 int count=18; 16 while (count-->0) 17 { 18 printf ("write thread2\n"); 19 sleep (1); &NBSP;20&NBSP;&NBSP;&NBsp; } 21 pthread_exit ((void*) 2); 22 23 } 24 void *write3 (Void*arg) 25 { 26 int count= 20; 27 while (count-->0) 28 { 29 printf ("write thread3\n"); 30 sleep (1); 31 } 32 } 33 34 int main () 35 { 36 pthread_t thread1=0; 37 pthread_t thread2=0; 38 pthread_t thread3=0; 39 int ret=pthread_create (& Thread1,null,write1,null); 40 ret=pthread_create (&thread2,NULL,write2, NULL); 41 &nbSp;ret=pthread_create (&thread3,null,write3,null); 42 43 int count=10; 44 while (count-->0) 45 { 46 printf ("main thread\n"); 47 sleep (1); 48 } 49 void *thread_wait=0; 50 ret=pthread_join ( thread1,&thread_wait); 51 if (ret==0) 52 { 53 printf ("write thread1 wait%u success: %d\n ", (Unsigned long) thread1, (int) thread_wait); 54 55 } 56 else 55 } 56 else&Nbsp;57 { 58 printf (" Write error%s\n ", strerror (ret)); 59 } 60 ret=pthread_join (thread2,&thread_wait); 61 if (ret==0) 62 { 63 printf ("Write thread2 wait%u success: %d\n ", (Unsigned long) thread2, (int) thread_wait); 64 65 } 66 else 67 { 68 printf ("write erroe%s\n" , Strerror (ret)); 69 } 70 pthread_cancel ( THREAD3); 71 ret=pthread_join (thread3,&thread_wait); 72 if (ret==0) 73 { 74 printf ("Write thread3 wait%u success: %d\n ", (Unsigned long) thread3, (int) thread_wait); 75 76 } 77 else 78 { 79 printf ("write erroe%s\n" , Strerror (ret)); 80 } 81 return 0; 82 }
Execution results are as follows
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/7F/13/wKioL1cTDsehjGm8AACGwGu4bf8045.png "title=" Untitled. png "alt=" Wkiol1ctdsehjgm8aacgwgu4bf8045.png "/>
Separation of Threads
Since the thread is created to wait for it, we can detach it by thread in order to let the created thread run its own way.
Threads created by default are all associative, while Pthread_detach can be used to detach threads;
Detach the main process without waiting for it, it will automatically release all resources so that the master process can continue to run the following program
3 void * pthread1 (Void *arg) 4 { 5 pthread_detach (Pthread_self ()); 6 int count= 5000; 7 while (count-->0) { 8 printf ("hello\n"); 9 sleep (1); 10 } 11 printf ("hello\n"); 12 } 13 int main () 14 { 15 pthread_t thread1=0; 16 int ret=pthread_ Create (&thread1,null,pthread1,null); 17 int count=5; 18 while (count-->0) { 19 printf ("main hello\n"); &nbsP;20 sleep (1); 21 } 22 void *thread_id=0; 23 ret=pthread_join (thread1,&thread_id); 24 printf ("%d\n", THREAD_ID); 25 printf ("11111\n"); 26 sleep (1); 27 return 0; 28 } ~ ~ "THREAD_DETACH.C" 28L, 488C written 7,17-20 Bottom
This article is from the "Traces" blog, be sure to keep this source http://wpfbcr.blog.51cto.com/10696766/1764723
Control and separation of threads