Threads and Concurrency Summary

Overview

  • Most modern applications are multithreaded.

  • Threads run within applications, allowing multiple tasks to be implemented separately.

  • Process creation is heavy-weight, while thread creation is light-weight.

  • Kernels are generally multithreaded.

Benefits of Multithreading

  • Responsiveness: Allows continued execution if part of the process is blocked.

  • Resource Sharing: Threads share resources of the process.

  • Economy: Cheaper than process creation; lower overhead than context switching.

  • Scalability: Can take advantage of multicore architectures.

Multicore Programming

  • Parallelism implies a system can perform more than one task simultaneously.

  • Concurrency supports more than one task making progress (on a single core, achieved by the scheduler).

Types of Parallelism

  • Data parallelism: Distributes subsets of the same data across multiple cores, performing the same operation on each.

  • Task parallelism: Distributes threads across cores, each thread performing a unique operation.

User Threads vs. Kernel Threads

  • User threads: Management done by user-level threads library.

    • Examples: POSIX Pthreads, Windows threads, Java threads.

  • Kernel threads: Supported by the Kernel.

    • Examples: Windows, Linux, Mac OS X, iOS, Android.

Multithreading Models

  • Many-to-One: Many user-level threads mapped to a single kernel thread. One thread blocking causes all to block.

  • One-to-One: Each user-level thread maps to a kernel thread. More concurrency than many-to-one.

  • Many-to-Many: Allows many user-level threads to be mapped to many kernel threads.

  • Two-level Model: Similar to M:M, but allows a user thread to be bound to a kernel thread.

Thread Libraries

  • Provide programmers with APIs for creating and managing threads.

  • Implemented either in user space or as a kernel-level library.

Operating System Examples

Windows Threads:

  • Implements the one-to-one mapping, kernel-level.

  • Each thread contains: thread ID, register set, separate user and kernel stacks, and a private data storage area.

  • Register set, stacks, and private storage area constitute the thread's context.

Java Threads:

  • Managed by the JVM.

  • Typically implemented using the threads model provided by the underlying OS.

  • Created by extending the Thread class or implementing the Runnable interface.

Linux Threads:

  • Referred to as tasks rather than threads.

  • Thread creation done through the clone() system call.

  • clone() allows a child task to share the address space of the parent task (process).

Word Example

  • When you are typing, common typos are corrected (hte -> the).

  • automatic spell checker underlines words that are misspelled.

  • auto-formats what you type into numbered or bulleted lists its printing out.

  • auto-saves the current version.