In computing, POSIX Threads, commonly known as pthreads (after its header ), is an execution model that exists independently from a programming language, as well as a parallel execution model. It allows a Computer program to control multiple different flows of work that overlap in time. Each flow of work is referred to as a thread, and creation and control over these flows is achieved by making calls to the POSIX Threads API. POSIX Threads is an API defined by the Institute of Electrical and Electronics Engineers (IEEE) standard POSIX.1c, Threads extensions (IEEE Std 1003.1c-1995).
Implementations of the API are available on many Unix-like POSIX-conformant such as FreeBSD, NetBSD, OpenBSD, Linux, macOS, Android, Solaris, Redox, QNX, and AUTOSAR Adaptive, typically bundled as a library libpthread. DR-DOS and Microsoft Windows implementations also exist: within the SFU/SUA subsystem which provides a native implementation of a number of POSIX APIs, and also within third-party packages such as pthreads-w32, which implements pthreads on top of existing Windows API.
There are around 100 threads procedures, all prefixed pthread_ and they can be categorized into five groups:
The POSIX semaphore API works with POSIX threads but is not part of the threads standard, having been defined in the POSIX.1b, Real-time extensions (IEEE Std 1003.1b-1993) standard. Consequently, the semaphore procedures are prefixed by sem_ instead of pthread_.
void* perform_work(void* arguments){
int main(void) {
int index = *((int*)arguments);
int sleep_time = 1 + rand() % NUM_THREADS;
printf("Thread %d: Started.\n", index);
printf("Thread %d: Will be sleeping for %d seconds.\n", index, sleep_time);
sleep(sleep_time);
printf("Thread %d: Ended.\n", index);
return NULL;
}
pthread_t threads[NUM_THREADS];
int thread_args[NUM_THREADS];
int result_code;
//create all threads one by one
for (int i = 0; i < NUM_THREADS; i++) {
printf("In main: Creating thread %d.\n", i);
thread_args[i] = i;
result_code = pthread_create(&threads[i], NULL, perform_work, &thread_args[i]);
assert(!result_code);
}
printf("In main: All threads are created.\n");
// wait for each thread to complete
for (int i = 0; i < NUM_THREADS; i++) {
result_code = pthread_join(threads[i], NULL);
assert(!result_code);
printf("In main: Thread %d has ended.\n", i);
}
printf("Main program has ended.\n");
return 0;
}
This program creates five threads, each executing the function perform_work that prints the unique number of this thread to standard output. If a programmer wanted the threads to communicate with each other, this would require defining a variable outside of the scope of any of the functions, making it a global variable. This program can be compiled using the gcc compiler with the following command:
gcc pthreads_demo.c -pthread -o pthreads_demo
Here is one of the many possible outputs from running this program.
The Mingw-w64 project also contains a wrapper implementation of 'pthreads, winpthreads, which tries to use more native system calls than the Pthreads4w project.see http://locklessinc.com/articles/pthreads_on_windows which is where it was originally derived from
Interix environment subsystem available in the Windows Services for UNIX/Subsystem for UNIX-based Applications package provides a native port of the pthreads API, i.e. not mapped on Win32 API but built directly on the operating system syscall interface.
|
|