2. The threads library allows concurrent programming in OCaml. It
provides multiple threads of control (also called lightweight
processes) that execute concurrently in the same memory space.
Threads communicate by in-place modification of shared data
structures, or by sending and receiving data on communication
channels.
The threads library is implemented by time-sharing on a single
processor. It will not take advantage of multi-processor machines.
Using this library will therefore never make programs run faster.
However, many programs are easier to write when structured as
several communicating processes.
Two implementations of the threads library are available, depending
on the capabilities of the operating system:
System threads. This implementation builds on the OS-provided
threads facilities: POSIX 1003.1c threads for Unix, and Win32
threads for Windows. When available, system threads support both
bytecode and native-code programs.
VM-level threads. This implementation performs time-sharing and
3. The POSIX (libpthread) routines introduced here have
programming interfaces that are similar to the original
(libthread) Solaris multithreading library.
The following brief roadmap directs you to the discussion of a
particular task and its associated man page.
"Create a Default Thread"
"Wait for Thread Termination"
"Detaching a Thread"
"Create a Key for Thread-Specific Data"
"Delete the Thread-Specific Data Key"
"Set the Thread-Specific Data Key"
"Get the Thread-Specific Data Key"
"Get the Thread Identifier"
"Compare Thread IDs"
"Initializing Threads"
"Yield Thread Execution"
4. Create a Default Thread
When an attribute object is not specified, it
is NULL, and the default thread is created with
the following attributes:
Unbound
Nondetached
With a default stack and stack size
With the parent's priority
You can also create a default attribute object
with pthread_attr_init(), and then use this
attribute object to create a default thread. See
the section "Initialize Attributes" for detail
5. A Simple Threads Example
In one thread executes the procedure at the top, creating
a helper thread that executes the procedure fetch(), which
involves a complicated database lookup and might take
some time.
The main thread wants the results of the lookup but has
other work to do in the meantime. So it does those other
things and then waits for its helper to complete its job by
executing pthread_join().
An argument, pbe, to the new thread is passed as a stack
parameter. This can be done here because the main thread
waits for the spun-off thread to terminate. In general,
though, it is better to use malloc(3C) to allocate storage
from the heap instead of passing an address to thread
stack storage, because this address might disappear or be
reassigned if the thread terminated.
6. Signal handling
Signals are used in UNIX systems to notify a process that a particular
event has occurred
A signal handler is used to process signals
1.Signal is generated by particular event
2.Signal is delivered to a process
3.Signal is handled
Options:
◦ Deliver the signal to the thread to which the signal applies
◦ Deliver the signal to every thread in the process
◦ Deliver the signal to certain threads in the process
◦ Assign a specific threa to receive all signals for the process
Thread pools
Create a number of threads in a pool where they await work
Advantages:
◦ Usually slightly faster to service a request with an existing thread than
create a new thread
◦ Allows the number of threads in the application(s) to be bound to the
size of the pool
7. Thread specific data
Allows each thread to have its own copy of data
Useful when you do not have control over the
thread creation process (i.e., when using a thread
pool)
Scheduler activations
Many:Many models require communication to
maintain the appropriate number of kernel
threads allocated to the application
Scheduler activations provide upcalls - a
communication mechanism from the kernel to
the thread library
This communication allows an application to
maintain the correct number kernel threads
8. PThreads
A POSIX standard (IEEE 1003.1c) API for thread creation and
synchronization
API specifies behavior of the thread library, implementation is up
to development of the library
Common in UNIX operating systems (Solaris, Linux, Mac OS X)
Windows Threads
Implements the one-to-one mapping
Each thread contains
◦ A thread id
◦ Register set
◦ Separate user and kernel stacks
◦ Private data storage area
The register set, stacks, and private storage area are known as
the context of the threads
9.
10. 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 parent
task (process)
Java Threads
Java threads may be created by:
◦ Extending Thread class
◦ Implementing the Runnable interface
Java threads are managed by the JVM.
11. Responsiveness
Resource Sharing
Economy
Utilization of MP Architectures
Thread management done by user-level threads library
Examples
- POSIX Pthreads
- Mach C-threads
- Solaris threads
• Supported by the Kernel
• Examples
- Windows 95/98/NT/2000
- Solaris
- Tru64 UNIX
- BeOS
12. • Supported by the Kernel
• Examples
• - Windows 95/98/NT/2000Supported by the Kernel
• Examples
- Windows 95/98/NT/2000
- Solaris
- Tru64 UNIX
- BeOS
- Linux
- Solaris
- Tru64 UNIX
- BeOS
- Linux
13. Each user-level thread maps to kernel thread.
Examples
- Windows 95/98/NT/2000
- Linux
14. Semantics of fork() and exec() system calls
Does fork() duplicate only the calling thread or all threads?
Thread cancellation
Terminating a thread before it has finished
Two general approaches:
◦ Asynchronous cancellation terminates the target thread
immediately
◦ Deferred cancellation allows the target thread to
periodically check if it should be cancelled
15. Modern operating systems have multi-threaded processes.
A program starts with one main thread, but once running it may
create more threads.
Threads may enter the kernel (e.g., syscall).
Threads are known to the kernel and have separate kernel
stacks, so they can block independently in the kernel.
◦ Kernel has syscalls to create threads (e.g., Linux clone).
Inherent costs of kernel threads
◦ Cost of accessing thread management functions.
Kernel trap
Copying and checking parameters
Costly compared to user level.
◦ Cost of generality
Thread management is not inherently expensive.
◦ Can be within order of magnitude of procedure call.
◦ So overhead added by kernel is significant.