![pthread c programming pthread c programming](https://images.slideplayer.com/28/9380220/slides/slide_3.jpg)
In our case the strings hello from 1/hello from 2 are passed to the say_hello function in the data variable. This same pointer is passes to the start_routine function when it is called. Is a void pointer which points to any data type. In our case it is the ever so friendly say_hello function. Since we have specified NULL the thread runs with default parameters. Is a variable of type pthread_attr_t, if specified it holds details about the thread, like scheduling policy, stack size etc. Is a pthread_t variable in our case t1 and t2. Int pthread_create(pthread_t *thread, const pthread_attr_t *attr,void *(*start_routine)(void*), void *arg) Each thread requires one pthread_t variable Pthread_t is a data type that holds information about threads. Needless to say that the function never returns. The function converts the data pointer to a string pointer str and runs a while loop to print it,the sleep function pauses the thread for 1 second as specified.
![pthread c programming pthread c programming](https://slidetodoc.com/presentation_image_h/14cc9c4506a29e87b30b4519bbf8a933/image-2.jpg)
![pthread c programming pthread c programming](https://www.modernescpp.com/images/blog/Threads/MultithreadingInModernC++/timeline.png)
The function needs to have a void* pointer as argument and must return a void* pointer ( void* can be interpreted as a pointer to anything ). In this case the function prototype is predefined. Include it to enable thread support for your C programĮach thread executes a function. The – lpthread at the end to link the pthread library Pthread_create(&t2,NULL,say_hello,"hello from 2") Pthread_create(&t1,NULL,say_hello,"hello from 1") They can work like two or more processes sharing the same address space ie they will work independently like processes but can share the same global variables.They are mostly used when two tasks can be done independently without depending much on each other.As an example your IM client might launch one thread for each conversation ( if I am talking to A and B, the two conversations are totally computationally independent from each other ). Threads are often described as light-weight processes.