6. Why should I care about Threads?
Pro
• Responsiveness
• Resources
sharing
• Economy
• Scalability
Cons
• Hard implementation
• Synchronization
• Critical section,
deadlock, livelock…
Introduction
8. Thread Models
User-level Threads
Implemented in software library
Pthread
Win32 API
Pro:
• Easy handling
• Fast context switch
• Trasparent to OS
• No new address
space, no need to
change address
space
Cons:
• Do not benefit from
multithreading or
multiprocessing
• Thread blocked
Process blocked
9. Thread Models
Kernel-level
Threads Executed only in kernel mode, managed
by OS
Kthreadd children
Pro:
• Resource Aware
• No need to use a
new address space
• Thread blocked
Scheduled
Con:
• Slower then User-
threads
28. Popping off thread #2 old
context
Single-core Vs Multi-core
Xthread_ctxtswitc
h:
pusha
movl esp, [eax]
movl edx, esp
popa
ret
CPU
ESP
Thread 2 regs
Thread 1 TCB
SP: ....
Thread 2 TCB
SP: ....
Thread 1
registers
Ready Running
29. Done: return
Single-core Vs Multi-core
Xthread_ctxtswitc
h:
pusha
movl esp, [eax]
movl edx, esp
popa
ret
CPU
ESP
Thread 2 regs
Thread 1 TCB
SP: ....
Thread 2 TCB
SP: ....
Thread 1
registers
Ready Running
RET pops of the
returning address
and it assigns its
value to PC reg
30. Problems
Critical Section:
When a thread A tries to access to a shared
variable simultaneously to a thread B
Deadlock:
When a process A is waiting for
resource reserved to B, which is
waiting for resource reserved to A
Race Condition:
The result of an execution depens on
the order of execution of different
threads
31. More Issues
fork() and exec() system calls: to duplicate or
to not deplicate all threads?
Signal handling in multithreading application.
Scheduler activation: kernel threads have to
communicate with user thread, i.e.: upcalls
Thread cancellation: termination a thread
before it has completed.
• Deferred cancellation
• Asynchronous cancellation: immediate
32. Designing a thread library
Multiprocessor support
Virtual processor
RealTime support
Memory Management
Provide functions library rather than a module
Portability
No Kernel mode
33. Implementation
Posix Thread
Posix standard for threads: IEEE POSIX
1003.1c
Library made up of a set of types and
procedure calls written in C, for UNIX
platform
It supports:
a) Thread management
b) Mutexes
c) Condition Variables
d) Synchronization between threads using
R/W locks and barries
34. Implementation
Thread Pool
Different threads available in a pool
When a task arrives, it gets assigned to a
free thread
Once a thread completes its service, it
returns in the pool and awaits another work.
35. Implementation
PThred Lib base operations
pthread_create()- create and launch a new thread
pthread_exit()- destroy a running thread
pthread_attr_init()- set thread attributes to their
default values
pthread_join()- the caller thread blocks and waits
for another thread to finish
pthread_self()- it retrieves the id assigned to the
calling thread
42. A case study
Using threads in Interactive Systems
• Research by XEROX PARC Palo Alto
• Analysis of two large interactive system: Cedar and GVX
• Goals:
i. Identifing paradigms of thread usage
ii. architecture analysis of thread-based environment
iii. pointing out the most important properties of an
interactive system
43. A case study
Thread model
Mesa language
Multiple, lightweight, pre-emptively scheduled threads in shared
address space, threads may have different priorities
FORK, JOIN, DETACH
Support to conditional variables and monitors: critical sections and
mutexes
Finer grain for locks: directly on data structures
44. A case study
Three types of thread
1. Eternal: run forever, waiting for cond. var.
2. Worker: perform some computation
3. Transient: short life threads, forked off by long-lived
threads
45. A case study
Dynamic analysis
0
5
10
15
20
25
30
35
40
45
Cedar GVX
# threads idle
Fork rate max
# threads max
Switching intervals: (130/sec, 270/sec) vs. (33/sec,
60/sec)
46. A case study
Paradigms of thread usage
Defer Work: forking for reducing latency
print documents
Pumps or slack processes: components of pipeline
Preprocessing user input
Request to X server
Sleepers and one-shots: wait for some event and then
execute
Blink cursor
Double click
Deadlock avoiders: avoid violating lock order constraint
Windows repainting
47. A case study
Paradigms of thread usage
Task rejuvenation: recover a service from a bad state,
either forking a new thread or reporting the error
o Avoid fork overhead in input event dispatcher of
Cedar
Serializers: thread processing a queue
o A window system with input events from many
sources
Concurrency exploiters: for using multiple processors
Encapsulated forks: a mix of previous paradigms, code
modularity
48. A case study
Common Mistakes and Issues
o Timeout hacks for compensate missing NOTIFY
o IF instead of WHILE for monitors
o Handling resources consumption
o Slack processes may need hack YieldButNotToMe
o Using single-thread designed libraries in multi-
threading environment: Xlib and XI
o Spurious lock
49. A case study
Xerox scientists’ conclusions
Interesting difficulties were discovered both in
use and implementation of multi-threading
environment
Starting point for new studies