Threading Flashcards

1
Q

how to pass data

A

by pointer
thread(func, &arg)
void* func(int* args) { ++(*args);}

by reference
thread(func, std::ref(arg))
void* func(int &args) { ++args;}

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

user level thread and system level

A

user level more simple, no os needed
but cant force it to give up hw

pro: lightweight
con: no scheduling guarantees

usecase: light fine grained tasks

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

yield and spin locks

A

spin
thread will keep probind until lock free
fast, but uses more resources hw

yield
if locked it gives up hw
low resources, but slow and bad for hpc

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

lock granulairyt

A

coarse: one lock for everythign
easy, but low concurrency

fine: one lock for each data element
good concurrency, but deadlcoking

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

how to avoid lock deadlock

A
  1. order locks
  2. hold 1 lock at a time
  3. have a central arbiter (overhead)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

code threads

A

include mutex
include thread
std::mutex mtx;

void func() {
mtx.lock;
var access
mtx.unlock(); }

main {
std::thread threads[10];
for (loop 10) {
threads[i] = std::thread(func, arg); }
for (auto &th: threads) {
th.join(); }

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