Threads and Mutex Flashcards

1
Q

What is a thread?

A

A thread is similar to a process: It can execute code but threads are within a process. The main difference is that threads share the same address spaces but each thread has its own stack

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

Why can the usage of threads be useful?

A
  • Parallel: Work parallel on big data
  • I/O: One thread can do I/O while the other is working somehting else
  • Periodical: Periodical work can be done by a thread in the background
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is a “critical section”?

A

A critical section is getting used by multiple threads/processes and is prone to race conditions. It must therefore be protected somehow.

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

What are 2 ways of dealing with critical sections? Which one is preferable?

A
  • Atomicity: Use HW based atomic operations
  • Synchronization primitives: Combination of OS and HW based operations to safe critical section.
    Atomicity is too costly, therefore the 2nd.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

For what do we use locks? (1 sentence)

A

To prevent a critical section from being executed by two threads.

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

How does a mutual exclusion lock (mutex) work?

A

2 stated:

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

What are coarse-grained and fine-grained locking strategies?
- What is the problem with larger ciritcal sections that gets locked?

A
  • coarse-grained: Include more code in the lock as critical section
  • fine-grained: Only include the absolute minimum of code as critical section
  • Locking large section normally reduces the concurrency (even until no more concurrency is doable)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

HW-based lock-support: Disabling Interrupts: Name 2 things why this is a bad idea.

A
  • OS looses control: If an endless loop is in the lock noone can help you.
  • Fails for multiple CPUs: Interrupts are per CPU
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

HW-based lock-support: A simple flag variable

  • How does it work?
  • What are the problems?
A
  • Uses a variable: If 0 it is free, if 1 it is taken. Method unlock spins until it is free.
  • Race conditions
  • Spin-waits: Uses CPU time basically for nothing
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the Test-and-Set Instruction?

A

The first atomic HW instruction for building locks. Returns old value and sets the pointer to new value.

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

HW-based lock-support: A valid spin lock

  • What is difference to a simple flag variable?
  • Does this fix all problems of a simple flag variable?
A
  • It uses an atomic instruction for the unlock function (to be more specific: It uses Test-and-Set)
  • It fixes the correctness issue, but it is still not fair (Thread may spin forever and starve).
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Explain the HW Operation “Compare-and-Swap”

A

Atomic operation, Compares a value to an expected value. If it is the same it replaces the value with another value.

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

Explain the HW Operation “Load-Linked, Store Conditional”

A

Two instructions work together atomically:

  1. LoadLinked: Loads a value
  2. StoreConditional: Only modifies the value if the value wasn’t modified since it was loaded by LoadLinked()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Explain the HW Operation “Fetch and Add”

A

Atomic operation, takes a value increments it and returns the old value

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q
  • How is a “Ticket Lock” implemented?

- Is it better than the other locks mentioned?

A
  • Lock has 2 variables: Turn and ticket (both 0)
  • Lock uses fetchAndAdd(ticket).
  • Yes because it is correct and because it provides fairness (Ticket system).
How well did you know this?
1
Not at all
2
3
4
5
Perfectly