Professional Documents
Culture Documents
Chapter 4: Threads
Overview
Multicore Programming
Multithreading Models
Thread Libraries
Implicit Threading
Threading Issues
Operating System Examples
4.2
Objectives
To introduce the notion of a threada fundamental unit of CPU
thread libraries
To explore several strategies that provide implicit threading
To examine issues related to multithreaded programming
To cover operating system support for threads in Windows and
Linux
4.3
Motivation
Most modern applications are multithreaded
Threads run within application
Multiple tasks with the application can be implemented by
separate threads
Update display
Fetch data
Spell checking
weight
Can simplify code, increase efficiency
Kernels are generally multithreaded
4.4
4.5
Benefits
Responsiveness may allow continued execution if part of
architectures
4.6
Multicore Programming
Multicore or multiprocessor systems putting pressure on
Dividing activities
Balance
Data splitting
Data dependency
simultaneously
Concurrency supports more than one task making progress
4.7
4.8
4.9
4.10
Amdahls Law
Identifies performance gains from adding additional cores to an
4.11
POSIX Pthreads
Windows threads
Java threads
Windows
Solaris
Linux
Tru64 UNIX
Mac OS X
4.12
Multithreading Models
Many-to-One
One-to-One
Many-to-Many
4.13
Many-to-One
Many user-level threads mapped to
4.14
One-to-One
Each user-level thread maps to kernel thread
Creating a user-level thread creates a kernel thread
More concurrency than many-to-one
Number of threads per process sometimes
Windows
Linux
4.15
Many-to-Many Model
Allows many user level threads to be
package
4.16
Two-level Model
Similar to M:M, except that it allows a user thread to be
IRIX
HP-UX
Tru64 UNIX
4.17
Thread Libraries
Thread library provides programmer with API for creating
4.18
Pthreads
May be provided either as user-level or kernel-level
A POSIX standard (IEEE 1003.1c) API for thread creation and
synchronization
Specification, not implementation
API specifies behavior of the thread library, implementation is up
4.19
Pthreads Example
4.20
Pthreads
Example (Cont.)
Pthreads
Example (Cont.)
4.21
9 th Edition
4. 21
4.22
4.23
4.24
Java Threads
Java threads are managed by the JVM
Typically implemented using the threads model provided by
underlying OS
Java threads may be created by:
4.25
4.26
4.27
Implicit Threading
Growing in popularity as numbers of threads increase,
Thread Pools
OpenMP
4.28
Thread Pools
Create a number of threads in a pool where they await work
Advantages:
4.29
OpenMP
4.30
Signal Handling
1.
2.
3.
default
2.
user-defined
4.31
4.32
Thread Cancellation
Terminating a thread before it has finished
Thread to be canceled is target thread
Two general approaches:
4.33
I.e. pthread_testcancel()
4.34
Linux Threads
Linux refers to them as tasks rather than threads
Thread creation is done through clone() system call
clone() allows a child task to share the address space of the
(shared or unique)
4.35
End of Chapter 4