CPP multithreading Flashcards

1
Q

std::thread

A

Best way of using threads independently.

Works with both lamdas and functions:

void foo(int a, int b);

std: :thread t1(foo, 123, 456);
std: :thread t2([] { foo(123, 456); });

Join can be used to wait for a thread to finish

t1.join(); t2.join();

Threads are copyable, not movable, like futures.

Thread pools:

vector<thread> threadPool;</thread>

for(int i = 0; i < 10; i++)

threadPool.emplace_back([i] {safe_print(i); });

for(auto& t: threadPool)

t.join();

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

Mutexes

A

Important piece but expensive.

Several types:

  • mutex
    • has RAII wrapper: unique_lock
      • cool, it is released upon destruction.
      • example:
        { unique_lock l{m}; //or unique_lock l(m, defer_lock); and then lock(l); }
  • recursive_mutex
    • example:
      foo(){ m.lock(); bar(); m.unlock();}
      bar() {m.lock(); /*do something */ m.unlock();}
  • shared_mutex
    • also has RAII wrapper: shared_lock
  • Avoiding deadlocks: respect the order
    • if you really don’t know how to come out of it: scoped_lock (RAII)
      • does fancy stuff (very slow) and makes sure there is no deadlock
        • { scoped_lock l{m1, m2, m3}; … }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Condition variables

A

std::condition_variable:

  • wait()
  • notify_one()
  • notify_all()

A mutex must be held when modifying the variables that can change truth of the condition.

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

atomic operations

A

Usually much more efficient then mutexes

std::atomic<t></t>

  • T load(): loads the value
  • void store(T desired): stores desired object
  • T exchange(T desired): stores objects and returns old value
  • if integral type:
    • fetch_add
    • fetch_sub
    • fetch_or
    • fetch_xor
    • fetch_and
How well did you know this?
1
Not at all
2
3
4
5
Perfectly