Monday 26 March 2012

Moncler JacketsThe pthread_join functionand Linux thread pthread_join enables a thread to wait fo

The pthread_join functionand Linux thread pthread_join enables a thread to wait for another thread end .If no pthread_join code in the main thread will soon end so as to make the whole process ends ,Moncler Online,so that the threads do not have the opportunity to begin execution ended .
After joining pthread_join, the main thread will wait until the waiting thread end to end, so that the threads have the opportunity to perform .All thread has a thread number ,namely Thread ID .
It is of pthread_t type .By calling the pthread_self ( ) function can obtain its own thread .Said the following about how to create a thread .Through the creation of threads ,thread will execute a thread ,the thread must be in accordance with the following statement :format to void * Thread_Function ( void * ) to create a thread function as follows :int pthread_create ( pthread_t * restrict thread ,const pthread_attr_t * restrict attr ,void * ( start_routine ) ( void * ) ,void * restrict ARG ) the following parameters ;explain the meaning: Thread :threads created by a number .
Attr :create thread attribute ,this will be in the back of a detailed description of .Start_routine : the running thread function .Art :passed to a thread function parameters .The following is a simple example :create a thread # include < ;pthread.
h> include < ;# ;stdio.h> ;/ * Prints x&rsquo ;s to stderr . The parameter is unused . Does not return. * / void * print_xs ( void * Unused ) {while ( 1)( &lsquo ;x&rsquo ;fputc ,stderr ) ;return NULL ;} / * The main program * / int main ( ) {pthread_t thread_id ;/ * Create a new thread .
The new thread will run the print_xs function. * / pthread_create ( & ;thread_id ,NULL ,& ;print_xs ,NULL ) ;/ * Print o&rsquo ;s continuously to stderr. * / while ( 1)fputc ( &lsquo ;o&rsquo ;stderr ;0,return ) ;} at compile time need to pay attention to ,because the thread create function in the libpthread.
so library, so the compiler command requires the library into .The command is as follows: GCC &ndash ;O createthread &ndash ;lpthread createthread.c if you want to pass as a parameter to the thread function ,the parameter Arg ,its type is void * .
If you need to pass multiple parameters, these parameters can be considered to be composed of a structure to pass .In addition ,due to the type void * ,so you can be released early fall parameters .
The following is a problem and in front of the creation process is similar ,but the problem is much more serious withdrawal process .If your main thread ,is the main function performed by the thread ,Louboutin Shoes,in your other county before the launch had quit ,so the bug is inestimable .
Through the pthread_join function to make the main thread occlusion ,until all threads have quit .Int pthread_join ( pthread_t thread ,void * * value_ptr ) ;Thread :wait for exit thread number .
Value_ptr :exit the thread return value .Here is an example combined with the above contents: int main ( ) {pthread_t thread1_id ;pthread_t thread2_id ;struct char_print_parms thread1_args ;struct char_print_parms thread2_args ;a new thread / * Create to print 30000 x’s.
* / thread1_args.character = &rsquo ;x&rsquo ;pthread_create ;thread1_args.count = 30000 ;( & ;thread1_id ,NULL ,& ;char_print ,& ;thread1_args ) ;/ * Create a new thread to print 20000 o’s.
* / thread2_args.character = &rsquo ;o&rsquo ;pthread_create ;thread2_args.count = 20000 ;( & ;thread2_id ,NULL ,& ;char_print ,& ;thread2_args ) ;/ * Make sure the first thread has finished.
* / pthread_join ( thread1_id ,NULL ) ;/ * Make sure the second thread has finished. * / pthread_join ( thread2_id ,NULL ) ;/ * Now we can safely return. * / return 0 ;} said the following about the previously mentioned thread attribute .
In our earlier ,through the pthread_join ( ) function to make the main thread blocking other threads waiting back out ,so that the main thread can clear the other thread environment .But there are a number of threads ,more like myself to clean out of the state ,they are not willing to the main thread calls the pthread_join to wait for them .
I have this kind of thread attribute called detached .If we call pthread_create ( ) function when the property is set to NULL ,is that we hope to create a line process using a default property ,namely the jionable .
If you need to set properties to detached ,refer to the following example: # include < ;stdio.h> include < ;# ;pthread.h> ;void * start_run ( void * ARG ) { / / do some work} int main ( ) {pthread_t thread_id ;pthread_attr_t attr ;pthread_attr_init ( & ;attr ) ;pthread_attr_setdetachstate ( & ;attr ,PTHREAD_CREATE_DETACHED ) ;pthread_create ( & ;thread_id ,& ;attr ,start_run ,NULL ) ;pthread_attr_destroy ( & ;attr ) ;sleep ( Exit 5);( 0);} the thread is set to joinable ,you can call pthread_detach ( ) detached to become .
But the opposite operation can not .Also, if the thread is already calling pthread_join ( ) ,then calls the pthread_detach ( ) will not have any effect .A thread can be executed by the end to end ,or by calling pthread_exit ( ) to the end of execution of a thread .
In addition ,thread a thread end can be passive .This by calling pthread_cancel ( ) to achieve the purpose of .Int pthread_cancel ( pthread_t thread ) ;function call returns 0 on success .
Of course ,the thread is not passive being over .It can set their attributes to decide how to end .The passive end thread is divided into two kinds, one kind is asynchronous termination ,another is the synchronous end .
Asynchronous termination is when other threads to call pthread_cancel, thread was soon over .The synchronous end is not immediately end ,it will continue to run ,until the next end points ( cancellation point ) .
When a thread is created according to the default way to create, it is the property of synchronous end .By calling pthread_setcanceltype ( ) to set the end state .Int pthread_setcanceltype ( int type ,int * oldtype ) ;state :to set up the state ,Vibram Five Fingers,can be PTHREAD_CANCEL_DEFERRED or PTHREAD_CANCEL_ASYNCHRONOUS .
As mentioned before the end point is how to set ?The most commonly used to create the end point is to call pthread_testcancel ( ) .This function in addition to checking the synchronization at the end of the state ,don .
Above a function is used to set the end state .Also can pass the following function to set the end of types ,namely the thread can not can be ended :int pthread_setcancelstate ( int state ,int * oldstate ) ;state :end state ,can be PTHREAD_CANCEL_DISABLE or PTHREAD_CANCEL_ENABLE .
What we can through the word meaning can understand .Finally said that the essence of thread .In fact, in the Linux, the newly created thread is not in the original process,Beats By Dre Sale, but the system through a system call clone ( ) .
The system of copy a and the original process exactly the same process ,Moncler Jackets,and in this process the thread of execution function .But this process is not the same as fork copy .The copy process and the original process shared by all variables ,running environment .
Related articles:

No comments:

Post a Comment