Threads
16 important questions on Threads
What is a thread?
-> it comprises a thread ID, a program counter, a register set and a stack
It shares its code section, data section, and other OS resources, such as open files, with other threads belonging to the same process
Why use a thread over a process?
- Threads can simplify code, increase efficiency
What are 4 benefits of threads?
- Responsiveness - may allow continued execution if part of process is blocked, especially important for user interfaces
- Resource sharing - threads share resources of process, easier than shared memory or message passing
- Economy - cheaper than process creation, thread switching lower overhead than context switching
- Scalability - process can take advantage of multiprocessor architectures
- Higher grades + faster learning
- Never study anything twice
- 100% sure, 100% understanding
What is multicore programming (or multiprocessing)?
What are 2 types of parallelism?
2. Thread parallelism -> distributing threads across cores, each thread performing unique operation
What is the many-to-one model?
- one thread blocking causes all the threads to block
- multiple threads may not run in parallel on multicore system because only one may be in kernel at a time
(few systems use this model)
What is the one-to-one model?
- creating a user-level thread creates a kernel thread
- more concurrency than many-to-one
- Number of threads per process sometimes restricted due to overhead
What is a thread library?
Two ways of implementing:
- library entirely in user space
- Kernel-level library supported by OS
What are the 3 main thread libraries in use today?
- POSIX Pthreads (The Portable Operating System Interface)
- Windows threads
- Java threads
What is the Pthread library?
- common in UNIX operating systems
- API specifies behaviour of the thread library, implementation is up to the development of the library
What are Java threads?
- typically implemented using the threads model provided by underlying OS
- Java threads may be created by extending thread classes or implementing the Runnable interface
What is implicit threading?
--> implicit threading is growing in popularity as numbers of threads increase, program correctness is more difficult with explicit threads
What are 3 implicit threading methods?
- Thread pools
- Open MP
- Grand Central Dispatch
What is the thread pools method and what are its advantages?
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
- Separating task to be performed from mechanics of creating task allows different strategies for running task (i.e. tasks could be scheduled to run periodically)
What is openMP method?
Provides support for parallel programming in shared-memory environments
Identifies parallel regions (blocks of code that can run in parallel)
What is the grand central dispatch method?
- allows identification of parallel sections
- manages most of the details of threading
- blocks placed in dispatch queue --> assigned to available thread in thread pool when removed from queue
The question on the page originate from the summary of the following study material:
- A unique study and practice tool
- Never study anything twice again
- Get the grades you hope for
- 100% sure, 100% understanding