Download presentation
Presentation is loading. Please wait.
1
Multithreading Tutorial
Professor: Shu-Ching Chen TA: Yimin Yang
2
What is a Thread? An independent stream of instructions that can be scheduled to run A path of execution int a, b; int c; a = 1; b = a + 2; c = 3; Sequentially execute c = 5; CPU Multi-Thread Single-Thread
3
Program v.s. Process v.s. Thread
An execution file stored in the harddrive Process An execution file stored in the Memory Thread An execution path of part of the process HDD Memory Program Process Thread
4
Why is thread? Parallel execution Shared resources
Easier to create and destroy than processes (100X) Easy porting to multiple CPUs
5
The Pthread API Standardized C language threads programming interface for UNIX systems Four major groups Thread management: Routines that work directly on threads - creating, detaching, joining, etc. Mutex: Routines that deal with synchronization. Mutex functions provide for creating, destroying, locking and unlocking mutexes. Condition variable: Routines that address communications between threads that share a mutex. Synchronization: Routines that manage read/write locks and barriers. For UNIX systems, a standardized C language threads programming interface has been specified by the IEEE POSIX c standard. Implementations that adhere to this standard are referred to as POSIX threads, or Pthreads. 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 - though this library may be part of another library, such as libc, in some implementations. 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.
6
Creating threads (1) pthread_create() Return 0 if OK, nonzero on error
Four Arguments Thread : A thread identifier Attr : A pointer to a thread attribute object Start_routine : A pointer to the function the thread executes Arg : The argument to the function By default, a thread is created with certain attributes. Some of these attributes can be changed by the programmer via the thread attribute object. pthread_attr_init and pthread_attr_destroy are used to initialize/destroy the thread attribute object. Other routines are then used to query/set specific attributes in the thread attribute object. Attributes include: Detached or joinable state Scheduling inheritance Scheduling policy Scheduling parameters Scheduling contention scope Stack size Stack address Stack guard (overflow) size
7
Creating threads (2) Single variable
8
Creating threads (3) Several variables
9
Terminating Threads (1)
pthread_exit() Return 0 if OK, nonzero on error Four ways of terminating a thread The thread returns from its starting routine The thread makes a call to the pthread_exit subroutine The thread is canceled by another thread The entire process is terminated If main() finishes first, without calling pthread_exit
10
Terminating Threads (2)
Example of pthread_exit()
11
Terminating Threads (3)
pthread_join() Return 0 if OK, nonzero on error Wait from other threads to terminate by calling it "Joining" is one way to accomplish synchronization between threads. The pthread_join() subroutine blocks the calling thread until the specified threadid thread terminates. The programmer is able to obtain the target thread's termination return status if it was specified in the target thread's call to pthread_exit(). A joining thread can match one pthread_join() call. It is a logical error to attempt multiple joins on the same thread.
12
Terminating Threads (4)
"Joining" is one way to accomplish synchronization between threads.
13
Other Thread functions
pthread_self() It returns the unique, system assigned thread ID of the calling thread pthread_detach() Return 0 if OK, nonzero on error It can be used to explicitly detach a thread
14
Synchronizations Join Mutexes Condition variables
A join is performed when one wants to wait for a thread to finish. A thread calling routine may launch multiple threads then wait for them to finish to get the results. One waits for the completion of the threads with a join. mutex variables - mutual exclusion condition variables - to name events of interest
15
Synchronization - Mutexes (1)
Mutexes are used to prevent data inconsistencies due to operations by multiple threads upon the same memory area performed at the same time to prevent race conditions where an order of operation upon the memory is expected mutexes has to do with thread synchronization (simultaneous running threads) and the protection of critical code segments (shared data). Block access to variables by other threads. This enforces exclusive access by a thread to a variable or set of variables.
16
Synchronization - Mutexes (2)
If register load and store operations for the incrementing of variable counter occurs with unfortunate timing, it is theoretically possible to have each thread increment and overwrite the same variable with the same value. Another possibility is that thread two would first increment counter locking out thread one until complete and then thread one would increment it to 2.
17
Synchronization - Mutexes (3)
18
Synchronization – Condition Variables (1)
Condition variables are used to allow threads to synchronize based upon the actual value of data without continually polling to check whether the condition if met in conjunction with a mutex lock Condition variables provide yet another way for threads to synchronize. While mutexes implement synchronization by controlling thread access to data, condition variables allow threads to synchronize based upon the actual value of data. A CV may come very useful for waking up a sleeping (blocked) thread, e.g. signaling data availability etc. as a tool for simple inter-thread communication. This instead of e.g. using shared memory if the destination thread is supposed to operate short-term and then go back to sleep. Waiting on a CV is a blocking operation so the thread would only consume CPU when there is actually data available to operate on.
19
Synchronization – Condition Variables (2)
20
References POSIX Threads Programming Pthreads primer
Pthreads primer POSIX thread (pthread) Tutorial
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.