Posts

Showing posts with the label thread

4 Thread joining in Linux

4 Thread joining in Linux Program which contain in the post this and this main doesnt wait for thread finishes their work.  We can make the main thread wait for the child thread to finish their work with the help of thread join mechanism. We need to set thread as joinable by using thread attribute. API used for thread join :  int pthread_attr_init ( pthread_attr_t * attr ) ; This API used to init thread attribute. Pass pthread_attr_t type variable as argument. int pthread_attr_setdetachstate ( pthread_attr_t * attr , int detachstate ) ; Here only we need to set the properties of attribute variable. First argument is  pthread_attr_t type variable and second argument is state. There is two type of state PTHREAD_CREATE_DETACHED -  Threads that are  created  using  attr  will  be  created  in  a   detached state. PTHREAD_CREATE_JOINABLE -  Threads  that  are created using attr...

3 Passing arguments to thread in Linux

3 Passing arguments to thread in Linux Program contain in the previous thread post tell how to pass single argument to thread handler function.  If you want to pass two or more variable to thread handler then need to define structure globally then pass the structure variable as argument when creating thread with the help of pthread_create. program below will explain how to pass two or more variable to thread with the help  of structure. PROGRAM :  #include <stdio.h> #include <stdlib.h> #include <pthread.h> void * t_function ( void * ) ;   typedef struct _thread_stru {     int var1 ;     char var2 ;     int var3 ; } thread_stru ; thread_stru thrd_arg ;   void * t_function ( void * t_arg ) {     thread_stru * data ;     int y = 0 ;     data = t_arg ;     printf ( "Arguments -> %d %c %d " , data -> var1 , data -> var2 , data -> var3 ) ; ...