Module 1: Threading Flashcards

1
Q

Concurrency

A
  • Two or more tasks seem to execute at the same time.
  • Share the computer’s CPU.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Task

A

Unit of a program that can be executed concurrently with other units of a program.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Parallell programming

A
  • Two or more tasks run exactly at the same time.
  • Don’t share the same CPU.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Concurrency in an application is achieved as a collaboration between three components…

A

Underlying hardware: must support multithreading/processing.
Operating system: Must also support multithreading.
Application: Designed & contain algorithms to make best use of underlying OS & hardware.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Multiprocessing

A

Using multiple processes (applications at run-time)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Multithreading

A

Using multiple threads withing a process.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Application

A
  • When an application starts running = becomes process.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Scheduler

A
  • 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Interrupts

A
  • 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Multitasking

A

OS quickly switches between computing tasks; gives impression of diff. applications executing multiple actions simultaneously.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Multithreading

A

Form of multitasking w/in application. Executes several tasks concurrently (at the same time).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Process

A
  • 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).
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Process and memory isolation

A
  • 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).
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Threads & Processes

A
  • 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Thread

A
  • 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).
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Single-threading

A

Single path of execution; Application run by a single thread. Runs sequentially.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Sequential application

A
  • Each process is a sequential application
    = sequence of statements executed one after another.
  • One single thread of execution.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Concurrent application

A
  • Multiple threads of execution.
  • Communication between processes = programmed using shared variables or message passing.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

Parallelism

A
  • 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

Context Switching

A
  • 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.
21
Q

Benefits multithreading

A
  • 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.
22
Q

When to use Multithreading?

A
  • 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.
23
Q

When not to use multithreading?

A
  • 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.
24
Q

Synchronization – why need it?

A
  • 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.
25
Q

Time-slicing

A
  • 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.
26
Q

Stack memory

A
  • 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.
27
Q

Heap memory

A
  • Memory allocated by programmer. No restriction in size.
  • Can grow and shrink in memory during execution = dynamic.
28
Q

Where can parallell programming occur?

A
  • Distributed systems.
  • Real-time systems.
  • Multithreaded systems.
29
Q

Distributed system

A
  • Collection of independent computers that appears to its users as a single coherent system.
  • All communication is done thry message passing btw processes.
30
Q

Real-time system

A
  • Time is the main constraint.
  • Done using threads or processes.
31
Q

Drawbacks of multithreading

A
  • 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.
32
Q

Concurrent programming Risks

A

EX: Therac-25
Computer-controlled radiation therapy machine
At least 6 accidents btw 1985-1987
Patients given massive overdoses of radiation

33
Q

Thread States (life cycle)

A
  • 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.
34
Q

Thread running & CPU time

A
  • 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
35
Q

Thread Blocking

A
  • 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.
36
Q

Thread.Sleep

A
  • 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.
37
Q

Foreground thread

A
  • 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.
38
Q

Background thread

A
  • 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
39
Q

Starting a thread

A

threadName.Start();

40
Q

Join

A
  • 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.
41
Q

Killing a thread

A

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).
42
Q

Thread priority

A
  • 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
43
Q

Create and start a thread

A

Thread t2 = new Thread(new ThreadStart(StartClock));
t2.Start();

44
Q

The Start Method

A
  • Thread does not start unless Start() is called
  • Method is called on a new thread & thread executes and is alive until method completes.
45
Q

ParameterizedThreadStart

A
  • 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!!
46
Q

Out-dated ways of stopping threads

A
  • Abort, Suspend, Interrupt => Big NO NOs!
  • Raises exceptions. Can go out of control if not handled properly.
47
Q

Changing background thread to foreground thread

A

if(t2.IsBackground)
t2.IsBackground = false;

48
Q

Useful members of the Thread class

A
  • IsAlive = determines whether thread is still running.
  • Name = assigns name to a thread or retrives the name.
  • ManagedThreadId = returns id of thread.