Threads Flashcards
What is multithreading?
The ability of an OS to support multiple,
concurrent paths of execution within a single process.
What does a thread contain?
- Thread ID
- Process Counter
- Register Stack
- Stack
What do all threads in the same process share?
- Code Section
- Data Section - objects, open files, network connections, etc
What are the benefits of threads?
- Responsiveness
- Resource sharing
- Cost
- Scalability
- Reduces programming complexity
Parallelism vs Concurrency
Parallelism - A system is parallel if it can perform more than one task at a time
Concurrency - A concurrent system supports more than one task by allowing all the tasks to make progress
What are the challenges to parallel programming
- Pressure placed on system designers and application
programmers to better use multiple cores - System designers must write scheduling algorithms that use multiple processing core to allow parallel execution
- Challenge to modify existing programs
- Challenge to design new programs that are
multithreaded
Further challenges to parallel programming
- Identifying tasks: which areas of an application can be
divided into separate, concurrent (and ideally independent) tasks? - Balance: how do we balance the tasks on the multiple cores to achieve maximum efficiency?
- Data splitting: how can data sets be split for processing in parallel?
- Data dependency: Certain tasks will rely on data from
another. In this case, how do we synchronise the access to this data? - Testing and debugging: Due to complexities of concurrent and parallel execution (multiple pathways etc.), how do we debug such an application?
What are the model of multithreading?
- User threading which is above the kernel
- Kernel threading which is managed by the OS
There are 3 types of relationships between user and kernel threading
1. Many-to-one
2. One-to-one
3. Many-to-many
Many-to-one Model
Maps many user-level threads to one
kernel thread
Efficient
But, entire process will block if a
thread makes a blocking system call
lacks parallelism on multicore
systems
Systems need ability to take advantage of multiple cores
One-to-one Model
Maps each user thread to a kernel thread
It is more concurrent as it overcomes blocking issue
Allows for parallelism
But, for each user thread, there has to be a kernel thread
Many-to-many
A software developer can create as many user threads as necessary i.e. a Thread Pool
Corresponding kernel threads can run in parallel on a multiprocessor
If a thread blocks, kernel can schedule another for execution
How do we use threads?
- Through a thread library:
- POSIX Pthreads (user/kernel level)
- Windows (kernel)
- Java Threads (on top of Windows/Pthreads)
- API for creating and managing threads
Java Threads
Fundamental model of program execution in a Java program
Java API provides a rich feature set for the creation and management of threads
All programs consist of at least a single thread of control - main()
Java threads available on any system that contains a JVM
Which method for creating Java threads is better?
Implementing the Runnable interface is preferred over extending Thread class
What is Memory Consistency?
Memory consistency occurs when different threads have inconsistent views of the same data
Therefore, we need a happens-before relationship
which is to guarantee that memory writes by one statement are visible to another
To create a happens-before relationship, we can synchronize