POSIX Threads in OS (original) (raw)
Last Updated : 15 Apr, 2026
POSIX thread libraries (pthreads) are a standard C/C++ API used to create and manage threads for concurrent execution within a program. They allow multiple flows of execution to run in parallel, especially improving performance on multi-core or multiprocessor systems. Since threads share the same memory space and resources, they have lower overhead compared to creating separate processes (like using fork). Even on single-processor systems, threads help improve efficiency by utilizing waiting time during I/O operations.
To use PThreads, we must include the header at the start of the C/C++ program.
#include <pthread.h>
- PThreads (POSIX threads) is a standardized API for multithreading in C/C++ on Unix-like systems. It is defined by the POSIX standard, which specifies how operating systems should implement thread-related functionalities.
- PThreads provides a set of functions (APIs) for creating, managing, and synchronizing threads within a program.
**Uses
- Improves program performance through multithreading
- Enables concurrent execution of independent tasks
- Achieves parallelism on multiprocessor/multi-core systems
- Reduces overhead compared to process creation and management
- Provides efficient communication via shared memory
**Let's have a look at a C example of a better implementation approach
C `
#include <stdio.h> #include <stdlib.h> #include <pthread.h>
void *print_message_function(void *ptr);
int main() { pthread_t GeeksforGeeks1, GeeksforGeeks2; char *message1 = "GeeksforGeeks 1"; char *message2 = "GeeksforGeeks 2"; int geeky1, geeky2;
geeky1 = pthread_create(&GeeksforGeeks1, NULL, print_message_function, (void*)message1);
geeky2 = pthread_create(&GeeksforGeeks2, NULL, print_message_function, (void*)message2);
pthread_join(GeeksforGeeks1, NULL);
pthread_join(GeeksforGeeks2, NULL);
printf("GeeksforGeeks 1 returns: %d\n", geeky1);
printf("GeeksforGeeks 2 returns: %d\n", geeky2);
return 0;}
void *print_message_function(void *ptr) { char *message = (char *)ptr; printf("%s\n", message); return NULL; }
`
This program creates two threads, each executing a worker function that prints its unique identifier to the standard output. If interaction between threads is required, a global variable defined outside all functions can be used for shared access. The program can be compiled using the GCC compiler with the appropriate Pthreads library.
gcc pthreads_demo.c -lpthread -o pthreads_demo
- **gcc: GNU C Compiler used to compile the program
- **pthreads_demo.c: Source code file
- **-lpthread: Links the Pthreads library (required for thread functions)
- **-o pthreads_demo: Specifies the output executable file name
Windows Support for Pthreads
Pthreads are not natively supported on Windows. The Pthreads-w32 project provides a portable, open-source implementation that allows Unix-based applications using Pthreads to run on Windows with minimal changes. It supports 64-bit Windows systems with some additional setup.
Winpthreads, part of the MinGW-w64 project, is another implementation that uses more native Windows system calls, offering better integration and performance compared to Pthreads-w32.