In a Unix/Linux operating system, the C/C++ languages provide the POSIX thread(pthread) standard API(Application program Interface) for all thread related functions. It allows us to create multiple threads for concurrent process flow. It is most effective on multiprocessor or multi-core systems where threads can be implemented on a kernel-level for achieving the speed of execution. Gains can also be found in uni-processor systems by exploiting the latency in IO or other system functions that may halt a process.
We must include the pthread.h header file at the beginning of the script to use all the functions of the pthreads library. To execute the c file, we have to use the -pthread or -lpthread in the command line while compiling the file.
cc -pthread file.c or cc -lpthread file.c
The functions defined in the pthreads library include:
pthread_create: used to create a new thread
int pthread_create(pthread_t * thread, const pthread_attr_t * attr, void * (*start_routine)(void *), void *arg);
- thread: pointer to an unsigned integer value that returns the thread id of the thread created.
- attr: pointer to a structure that is used to define thread attributes like detached state, scheduling policy, stack address, etc. Set to NULL for default thread attributes.
- start_routine: pointer to a subroutine that is executed by the thread. The return type and parameter type of the subroutine must be of type void *. The function has a single attribute but if multiple values need to be passed to the function, a struct must be used.
- arg: pointer to void that contains the arguments to the function defined in the earlier argument
pthread_exit: used to terminate a thread
void pthread_exit(void *retval);
Parameters: This method accepts a mandatory parameter retval which is the pointer to an integer that stores the return status of the thread terminated. The scope of this variable must be global so that any thread waiting to join this thread may read the return status.
pthread_join: used to wait for the termination of a thread.
int pthread_join(pthread_t th, void **thread_return);
Parameter: This method accepts following parameters:
- th: thread id of the thread for which the current thread waits.
- thread_return: pointer to the location where the exit status of the thread mentioned in th is stored.
pthread_self: used to get the thread id of the current thread.
pthread_equal: compares whether two threads are the same or not. If the two threads are equal, the function returns a non-zero value otherwise zero.
int pthread_equal(pthread_t t1, pthread_t t2);
Parameters: This method accepts following parameters:
- t1: the thread id of the first thread
- t2: the thread id of the second thread
pthread_cancel: used to send a cancellation request to a thread
int pthread_cancel(pthread_t thread);
Parameter: This method accepts a mandatory parameter thread which is the thread id of the thread to which cancel request is sent.
pthread_detach: used to detach a thread. A detached thread does not require a thread to join on terminating. The resources of the thread are automatically released after terminating if the thread is detached.
int pthread_detach(pthread_t thread);
Parameter: This method accepts a mandatory parameter thread which is the thread id of the thread that must be detached.
Example: A simple implementation of threads may be as follows:
This line may be printed before thread terminates Threads are not equal Inside the thread This line will be printed after thread ends
Explanation: Here two threads of execution are created in the code. The order of the lines of output of the two threads may be interchanged depending upon the thread processed earlier. The main thread waits on the newly created thread for exiting. Therefore, the final line of the output is printed only after the new thread exits. The threads can terminate independently of each other by not using the pthread_join function. If we want to terminate the new thread manually, we may use pthread_cancel to do it.
Note: If we use exit() instead of pthread_exit() to end a thread, the whole process with all associated threads will be terminated even if some of the threads may still be running.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Mutex lock for Linux Thread Synchronization
- Get the stack size and set the stack size of thread attribute in C
- Print numbers in sequence using thread synchronization
- Static functions in C
- Write one line functions for strcat() and strcmp()
- Can static functions be virtual in C++?
- Functions that cannot be overloaded in C++
- Functions that are executed before and after main() in C
- Pure Functions
- Can virtual functions be inlined?
- Macros vs Functions
- fill() and fill_n() functions in C++ STL
- Return from void functions in C++
- Forward List in C++ | Set 1 (Introduction and Important Functions)
- Forward List in C++ | Set 2 (Manipulating Functions)
- Inbuilt library functions for user Input | scanf, fscanf, sscanf, scanf_s, fscanf_s, sscanf_s
- exec family of functions in C
- strdup() and strndup() functions in C/C++
- Functions in C/C++
- C | Functions | Question 11
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Akanksha_Rai