POSIX Thread Programming: Pthreads
POSIX Thread Programming: Pthreads
POSIX Thread Programming: Pthreads
https://computing.llnl.gov/tutorials/ pthreads/
What is a Thread?
An independent stream of instructions that can be scheduled to run as such by the operating system. To the software developer, the concept of a "procedure" that runs independently from its main program may best describe a thread.
What is a Process?
A process is created by the operating system,
requires a fair amount of "overhead". Processes contain information about program resources and program execution state Process ID, process group ID, user ID, and group ID , Environment , Working directory, Program instructions, Registers , Stack , Heap , File descriptors etc.
Thread - Concepts
This independent flow of control is accomplished because a thread maintains its own: Stack pointer Registers Scheduling properties (such as policy or priority) Set of pending and blocked signals Thread specific data.
Pthreads are defined as a set of C language programming types and procedure calls, implemented with a pthread.h header/include file and a thread library
Seeking Parallelism
Pipeline
a task is broken into a series of sub operations, each of which is handled in series, but concurrently, by a different thread. An automobile assembly line best describes this model.
Peer
similar to the manager/worker model, but after the main thread creates other threads, it participates in the work.
Thread Safeness
pthread API
Thread management:
Routines that work directly on threads - creating, detaching, joining, etc. They also include functions to set/query thread attributes (joinable, scheduling etc.)
Mutexes:
Routines that deal with synchronization, called a "mutex", which is an abbreviation for "mutual exclusion". Mutex functions provide for creating, destroying, locking and unlocking mutexes. These are supplemented by mutex attribute functions that set or modify attributes associated with mutexes.
Condition variables:
Routines that address communications between threads that share a mutex. Based upon programmer specified conditions. This group includes functions to create, destroy, wait and signal based upon specified variable values. Functions to set/query condition variable attributes are also included.
Synchronization:
Routines that manage read/write locks and barriers.
Conventions
Example call:
errcode = pthread_create(&thread_id; &thread_attribute &thread_function; &func_arg);
thread_id is the thread id or handle (used to halt, etc.) thread_attribute various attributes
standard default values obtained by passing a NULL pointer
thread_function the function to be run (takes and returns void*) func_arg an argument can be passed to thread_fun when it starts errorcode will be set nonzero if the create operation fails