Threads & Race conditions

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/15

encourage image

There's no tags or description

Looks like no tags are added yet.

16 Terms

1
New cards

What is a thread?

A lightweight execution unit inside a process: one independent path through its code

2
New cards

What is private to each thread?

Its ID, state, context (registers and program counter) and stack

3
New cards

What do all threads of a process share?

The code, global data and heap

4
New cards

Why is a thread switch faster than a process switch?

It does not switch the address space, though registers are still saved and loaded

5
New cards

What are the five thread states?

New, runnable, waiting or blocked, timed waiting, terminated

6
New cards

What is preemptive threading?

The runtime may hand control from one thread to another at any time, and higher priority gets precedence

7
New cards

What is non-preemptive threading?

Once a thread starts, it cannot be stopped or give up control until it finishes its task

8
New cards

What is a race condition?

The result depends on the timing and interleaving of threads, and some interleavings give a wrong answer

9
New cards

What is a data race?

Two or more threads access the same variable concurrently and at least one access is a write

10
New cards

Why is x = x + 1 not atomic?

It is three steps (load, add, store), and another thread can step in between them

11
New cards

Why can a race happen even on a single core?

A preemptive context switch can interleave two threads between any two instructions, so no parallelism is needed

12
New cards

What is a critical section?

Code where a data race may occur, such as an update to shared data

13
New cards

What are the three standard fixes for a critical section?

Atomize it, disable preemption inside it, or sequentialize access with locks or mutexes

14
New cards

Why is disabling preemption not enough?

It stops interleaving on one core but not threads running in parallel on other cores

15
New cards

Why does the sum example lock only the final update?

The partial sums are private and computed in parallel, so only the update of the shared total needs mutual exclusion

16
New cards

What is non-deterministic execution?

The order of operations is not fully fixed by the program, as in parallel programs