Chapter 4 Threads and Concurrency
- Thread runs within program.
- Process creation is heavy-weight, thread creation is light-weight.
- Simplify code, increase efficiency
Multi-threaded server architecture:
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Benefits
- Responsiveness: Even if a part of program is slow or blocked, other parts of the program can continue to run.
- Resource sharing: Threads shares memory and resources within process by default.
- Economy: Cheaper to create than process.
- Scalability: Runs in parallel in different cores.
Context switch VS thread switch
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
- Context switch will have to reload everything currently in memory space.
- Threads in the process shares the same code, data section and OS resources.
Multicore programming
Parallel
- Perform more than one task simultaneously.
Types of parallelism
- Data parallelism: Same data and operation on multiple core.
- Task parallelism: Distribute threads across cores, each performing an unique operation.
Amdahl's law
- S is serial portion of program.
- N processing cores
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
- As N approaches infinite, speedup approaches 1/S.
Concurrency
- Supports more than one task making progress.
- Single core multitask. Use a scheduler.
Multi-threading Models
- User threads: Management done by user-level thread library.
- POSIX thread
- Windows thread
- Java thread
- Kernel threads: Supported by the Kernel
Models
- Many-to-one
- Many user-level threads mapped to single kernel thread
- One thread blocking causes all to block
- May not run in parallel on multi-core system because only one thread can be in the kernel at a time.
- One-to-One
- Each user-level thread maps to kernel thread
- More concurrency than many-to-one
- Number of threads per process sometimes restricted due to overhead
- Many-to-many
- Allows many user level threads to be mapped to many kernel threads
- Allows the operating system to create a sufficient number of kernel threads
- Two level model
- Hybrid One-to-One and Many-to-many. Allows a user thread to be bound to kernel thread.
Thread Libraries
- Use thread libraries to create and and manage threads.
Pthreads
- Provided either as user-level or kernel-level.
- A POSIX standard (IEEE 1003.1c) API for thread creation and synchronization
- Is a spec. not implementation.
- API specifies behavior, implementation is up to library.
- Common in UNIX.
Example
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Windows multi-thread C program
Example
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
JAVA Threads
- Managed by the JVM
- JVM is available on any OSs, including Windows, Linux, MacOS.
- JAVA thread is created by:
- Extending the thread class
- implementing the runnable interface(Used most of the time)
Codes
- Implement runnable interface
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Java executor framework
- Java also allows thread creation around the Executor interface:
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
- The Executor is used as follows:
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Implicit threading
Thread pool
- Creates threads in a pool waiting for work.
Advantages
- Usually slightly faster than creating thread on request.
- Number to threads is bound to the size of pool
- task can start run by mechanics rather than create thread.
Windows API supports thread pool
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Three methods to create thread pool for JAVA
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →

Fork Join
- Threads are forked, and then joined.
- General algorithm for fork-join strategy
Fork join parallelism in JAVA


- The ForkJoinTask as an abstract task.
- RecursiveTask and RecursiveAction classes extend ForkJoinTask.
- RecursiveTask returns a result (via the return value from the compute() method)
- RecursiveAction does not return a result

OpenMP
- Identifies parallel regions as code blocks by
#pragma omp parallel
.


Grand Central Dispatch
- For MacOS and IOS.
- Extensions to C, C++ and Objective-C languages, API, and run-time library
- Allows identification of parallel sections by
^{}
.
- Blocks placed in dispatch queue. After task is removed from queue, assign the task to empty thread in thread pool.
- Two types of dispatch queues:
- Serial: Blocks removed in FIFO order. Queue is per process, called main queue.
- concurrent: Removed in FIFO order, several may be removed at a time.
Intel Threading Building Blocks (TBB)
- Template library for designing parallel C++ programs
- For loop written using TBB with parallel_for statement
- parallel_for(size_t(0),n, [=] (size_t i){apply(v[i])})
Threading Issues
The fork() and exec() System Calls
- Two versions of fork()
- Only duplicate the called thread
- Duplicate all threads
- Exec(). Replace all threads in process.
Signal handling
- Signal is used to notify an event has occurred.
- A signal handler is used to process signals.
- Signal is created by particular event.
- Signal is delivered to a process.
- Signal handlers can be default or user-defined.
- Every signal has default handler that kernel runs. User-defined signal handler can override default.
- Four types of signal deliver:
- 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 thread to receive all signals for the process
Thread Cancellation
- Terminate a thread before it has finished.
- Thread to be canceled is called the target thread.
Two general approaches
- Asynchronous cancellation: Terminates the target thread immediately.
- Deferred cancellation: Target thread periodically check if it should be cancelled.
Pthread example to create and cancel a thread.

pthraed_cancel(tid)
sends a request to terminate only. Actual cancellation depends on thread state.
- If thread has cancellation disabled, cancellation remains pending.
- Default type is deferred.
- Cancel only occurs when pthread_testcancel() is called, the cleanup handler is invoked.
JAVA
- Deferred cancellation uses the
interrupt()
method.

- A thread checks if it is interrupted.

Thread-Local Storage(TLS)
- TLS allows each thread to have its own data.
- Useful when using thread pool.
Scheduler Activations
- Many-to-many and two-level models require communication to maintain the appropriate number of kernel threads allocated to the application.
- Lightweight process (LWP) is an intermediate data structure between kernel and user thread.
- Appears to be a virtual processor user thread could be scheduled to run on.
- Each LWP is attached to kernel thread.
- The kernel informs an application about current event using upcalls provided by upcall handler in thread library.
- This communication allows an application to maintain the correct number kernel threads

Operating System Examples
Windows Threads

Linux Threads
- Refers to as tasks not threads.
- Thread creation is done by
clone()
.
clone()
allows a task to share address space with process controlled by flags.

struct task_struct
points to process data structures (shared or unique)
Review Questions
-
- What resources are used when a thread is created and a process is created?
How do they differ from those used when a process is created? Difference
-
- How does a multi-threaded web server work?
-
- What are the benefits of multi-threaded programming? Explain each item. Benefits
-
- What are the differences between the three multithreading models? Models
-
- Implement creating a thread by the following thread library,
including the critical functions, parameters, and related procedures.
a. Pthread b. Windows thread c. Java Thread
-
- How does implicit threading work?
Describe the three types of implementations.Thread pool, openMP and Grand Central Dispatch
-
- How a signal can be delivered in a multi-threaded program.
Describe the four options.Signal handling
-
- How do the two types of thread cancellation work? Thread Cancellation
-
- How does the scheduler of kernel thread work, including the upcall procedure? Scheduler
-
- What is the data structure of Windows thread?
Describe the content of each block of data structure. Window Thread