Transcript Document

SMP threads
an Introduction to Posix Threads
Technical Definition
1. Independent stream of instructions that can be scheduled to
run by an operating system
2. The smallest unit of processing that can be run by an
operating system
3. Typically a thread is contained within a process
Shared Memory API's
1. OpenMP
o An open source solution to the threaded problem
overseen by a board of industry heads
o an explicit set of compiler directives which require
compiler support
o A very programmer friendly API
o Less control over execution than other APIs
2. Posix threads
o A international standard
o A more complicated API base
o More control over execution than other APIs
3. Kthreads
o Kernel level implementation of threads
Shared Memory Model
Threads have their own private data and access to shared
memory of other threads in their process.
Structures of SMP Programs
How does Linux Handle Threading?
The kernel handles the creation of threads very similar to the
way it handles the creation of processes (the clone() function),
However the use of specific flags will describe what specific
resources are to be shared.
This means that processes are also threads and threads are
simply a special brand of process tied to another thread.
All processes and threads are of the type task_struct
Pthread API
• 100 different subroutines
• 9 different prefixes of subroutines
o pthread_
o pthread_attr_
o pthread_mutex_
o pthread_mutexattr_
o pthread_cond_
o pthread_condattr_
o pthread_key_
o pthread_rwlock_
o pthread_barrier_
pthread_create(
pthread_t *, const pthread_attr_t *, void*(*)(void *), void
*);
pthread_t * is a pointer to a pthread_t object you allocate to
your stack
pthread_attr_t is what kind of flags will be set that define how
this thread will behave with other threads in the process
(passing null is allowed)
void*(*)(void *) is the name of a function which will return a void
* and has only 1 argument, a void pointer
void * is a pointer to a structure of your arguments or a cast of
a single argument
pthread_attr_
pthread_attr_t is the data type that will be passed to
pthread_create to describe how the pthread should interact
with other threads
Generally the pthread_attr_t is allocated on the stack and
passing the address to your functions is acceptable
There are several different flags that can be passed into the
this attribute with several different functions.
The most useful of these being pthread_attr_init()
pthread_attr_setdetachstate() and
PTHREAD_CREATE_JOINABLE
Critical Sections
Critical Sections of code are defined as:
o Code paths that manipulate shared data
 This means we must protect our programs from having
multiple threads in their critical sections
Protecting Critical Sections
o Mutexes (Mutual Exclusion) works by accessing a shared
mutex which blocks when its already in use and acquires
when its not in use
o Semaphores work by letting decrementing a counter
(atomically) if that would put the semaphore to a negative
value it blocks and sleeps the process.
Questions?