Module 1: Threading Flashcards
Concurrency
- Two or more tasks seem to execute at the same time.
- Share the computer’s CPU.
Task
Unit of a program that can be executed concurrently with other units of a program.
Parallell programming
- Two or more tasks run exactly at the same time.
- Don’t share the same CPU.
Concurrency in an application is achieved as a collaboration between three components…
Underlying hardware: must support multithreading/processing.
Operating system: Must also support multithreading.
Application: Designed & contain algorithms to make best use of underlying OS & hardware.
Multiprocessing
Using multiple processes (applications at run-time)
Multithreading
Using multiple threads withing a process.
Application
- When an application starts running = becomes process.
Scheduler
- Program run by OS or runtime. Decides which process is allowed to run within next period of time.
- Preemptive: Priority-based. Gives every task exclusive access to computing resource for given time period, then switches to other task.
- Cooperative: Every task exclusive access to computing resource until task finishes or task yields access to the resource.
Interrupts
- EX: Modern printers
- Signal to processor emmitted by hardware or software; indicating event needs immediate attention.
- Can occur between processes. A process requests interrupt on another.
- Also known as: Exceptions.
Multitasking
OS quickly switches between computing tasks; gives impression of diff. applications executing multiple actions simultaneously.
Multithreading
Form of multitasking w/in application. Executes several tasks concurrently (at the same time).
Process
- Created when program is started by OS.
- Receives ID and memory space.
- Instance of an application being executed.
- Instructions in machine language.
- Stack memory (keep track of methods).
- Heap memory.
- Program counter (register).
- Other (eg. security info, state of process, etc).
Process and memory isolation
- Runs own address space managed by OS, totally isolated from other processes.
- Don’t share / access each other’s memory space.
- May or may not communicate w/ each other.
- Exchanging messages, IPC (managed by OS).
Threads & Processes
- Unit of execution (just like process).
- Runs w/in address space of a process. Executes of the whole or parts of a program.
- Shares process’ memory area.
- Every thread performs a task.
- Task = unit of work that is to be scheduled.
Thread
- Single sequential flow of control w/in a program.
- Light-weight process.
- Sequence of instructions, unit of work, executed independently by a program.
- Threads are parallel to each other. Each do its own job.
- Done = deleted.
- Own execution context. Own set of registers, stack but not own memory space.
- All threads w/in same process share: memory, program code & global data.
- Each process: at least one main thread. Can have many (multithreaded).
Single-threading
Single path of execution; Application run by a single thread. Runs sequentially.
Sequential application
- Each process is a sequential application
= sequence of statements executed one after another. - One single thread of execution.
Concurrent application
- Multiple threads of execution.
- Communication between processes = programmed using shared variables or message passing.
Parallelism
- A form of concurrency.
- Perform multiple tasks simultaneously.
- Achieved in an application by splitting it into smaller tasks, executed in parallel.
- Needs: 2 or more processing units, cores or CPUS.
- 2 or more tasks are executed simultaneously (in concurrent: tasks making progress at the same time).
- Single-core CPU: concurrency but not parallelism.
- Speeds up execution of eg. intense data processing.
Context Switching
- CPU store state of process/thread when moving from one thread to another. => costs time & performance
- Context Switching between processes: Costs more! Bc memory management.
- THREAD SWITCHING is cheaper since threads share memory.
Benefits multithreading
- Better use of multiple cores/CPUs; Single-threaded on double-core machine is only husing half of the available CPU.
- Better use of processor; One thread takes IO operation, others do other tasks.
- Improved responsiveness; UI more responsive when heavy parts are given to a thread.
- Complex computations executed better.
When to use Multithreading?
- Most applications use multithreading. (EX: auto-correct takes place while we are typing).
- Make use of multiple cores.
- Improve reponsiveness of webpage (EX: loading an image).
- Time-consuming tasks (EX: printing) assigned to a thread while main thread does other jobs.
When not to use multithreading?
- Don’t always improve performance. Can be slower.
- Consume resources: Large nr of threads take a lot of memory.
- Threads = expensive. Creation & deletion & change of context: take CPU time.
Synchronization – why need it?
- Biggest problem; threads share process’ memory. Can access mutable state of variable = making program work incorrectly.
- Access to shared data must be synced between threads.
Time-slicing
- Concurrency is accomplished through this.
- Process assigns certain periods of time to each task / process. Time is so short that it switches from one task to another.
- Gives illusion that tasks are running at the same time.
Stack memory
- Region of computer’s memory created & managed by processor in coop with operating system & compiler
- Method calls & local variables.
- Size = limited & known to the compiler.
- Programmer doesn’t allocate / deallocate static memory.
Heap memory
- Memory allocated by programmer. No restriction in size.
- Can grow and shrink in memory during execution = dynamic.
Where can parallell programming occur?
- Distributed systems.
- Real-time systems.
- Multithreaded systems.
Distributed system
- Collection of independent computers that appears to its users as a single coherent system.
- All communication is done thry message passing btw processes.
Real-time system
- Time is the main constraint.
- Done using threads or processes.
Drawbacks of multithreading
- Algorithm development; hard to select efective algorithm to avoid syn problems.
- Non-determinism; applications are non-deterministic. Unpredictable, unrepeatable. When and how long thread can execute is managed by CPU.
- Synchronization; Sharing resources can cause race-condition, deadlocks, livelocks, affect fairness & liveliness.
Concurrent programming Risks
EX: Therac-25
Computer-controlled radiation therapy machine
At least 6 accidents btw 1985-1987
Patients given massive overdoses of radiation
Thread States (life cycle)
- Exist in different states from time they’re created til they’re terminated.
- 4 typical states:
1. Ready (newly created, not started)
2. Running/Active (after start)
3. Blocked/waiting (for a resource)
4. Terminated (dead) - Created: Has to be started too.
- Thread dies when its done with its task. Can die of exception/unusual event.
Thread running & CPU time
- Thread runs when it gets CPU time
- Is runnable when it starts, but only executes when it gets CPU time
- A running thread can be; suspended (paused) or interrupted by hardware (ex printers) and software (scheduler)
- Resume work from where it left off when it gets CPU time again
- Blocked when waiting for a resource
Thread Blocking
- Thread unable to cont. its execution. Qaiting for some resource or condition to become true
- Blocked => temporarily suspended (waiting/sleeping) & other threads continue to execute.
- Has to get CPU time to resume.
Thread.Sleep
- Timed waiting state
- Making the processor time available to other threads
- Sometimes need to slow down execution in order to evaluate flow of execution
- Threads are paused by Thread.Sleep(amount of time in milliseconds).
- Sleep time over = enters runnable state. Resumes when it gets CPU time.
Foreground thread
- Keeps application alive until it finishes its task.
- Keep running even if main thread is done with its job & ready to exit or terminate: main thread has to wait for foreground to finish.
Background thread
- Runs behind the scenes
- EX: plays music in background while app is doing other stuff
- Automatically terminates when app exits, even if thread hasn’t completed its task.
- Can do garbage collection or run tasks w/ low priority
Starting a thread
threadName.Start();
Join
- Thread.Join()
- Current thread execution is paused until specified thread is terminated
- Let caller method wait until threads whose join method is called is terminated. Used when you want one thread to wait for completion of another.
- CAN’T USE WHEN: thread is Unstarted or is current thread.
- Safe: Calling directly after starting thread.
Killing a thread
Use a Boolean variable (a flag) or conditional statement.
while (isRunning){
//Code
}
- Threads terminate when they are done with their work. Not worth it to reuse threads. Create new ones when needed (but remember that threads cost time).
Thread priority
- Every thread has a priority. Higher priority = runs before and more frequently than threads with lower priority.
- Default priority is assigned to thread when created.
- Not guaranteed that CPU runs thread w/ higher priority. Process’ priority also plays a role.
- Setting priorities may cause starvation.
- Property Priority with set & get.
- ThreadPriority enum
- Highest: 4
- AboveNormal: 3
- Normal: 2
- BelowNormal: 1
- Lowest: 0
Create and start a thread
Thread t2 = new Thread(new ThreadStart(StartClock));
t2.Start();
The Start Method
- Thread does not start unless Start() is called
- Method is called on a new thread & thread executes and is alive until method completes.
ParameterizedThreadStart
- Pass one or more values (objects) to thread
- Thread t3 = new Thread(new ParameterizedThreadStart(StartClock));
t3.Name = “Auckland Time”;
t3.Start(clock3); //sending clock3 as a value
——————————————————————————–
void StartClock(object obj)
try
SetTime((Clock)obj) //Cast obj to correct type!!
Out-dated ways of stopping threads
- Abort, Suspend, Interrupt => Big NO NOs!
- Raises exceptions. Can go out of control if not handled properly.
Changing background thread to foreground thread
if(t2.IsBackground)
t2.IsBackground = false;
Useful members of the Thread class
- IsAlive = determines whether thread is still running.
- Name = assigns name to a thread or retrives the name.
- ManagedThreadId = returns id of thread.