Multi-threading

Multi-threaded concepts are important: e.g., atomics, locks, issues with different designs, how to make things thread safe. Cache locality is another huge thing these days. Asynchronous architectures and callbacks are what you will be dealing with every day.

"A computer program or subroutine is called reentrant if multiple invocations can safely run concurrently on a single processor system."

"Race condition: an unfortunate order of execution causes undesirable behaviour."

Starting in C++11, scoped static initialization is now thread-safe, but it comes with a cost: reentrancy now invokes undefined behavior.

Issues

DCLP

Deadlocks

Detecting deadlocks

Execution policy

Prevention

Mutex

See std::mutex or std::atomic in C++.

Exercise: write a producer-consumer solution.

See Mutex vs Semaphore

C++ threads

Launch with std::thread, attach with std::join. C++20 introduced std::jthread which automatically rejoins on destruction.

std::async runs the function f asynchronously (potentially in a separate thread which might be a part of a thread pool) and returns a std::future that will eventually hold the result of that function call. Not entirely intuitive, see std::async.

Threads versus processes

A thread is a branch of execution. A process can consist of multiple threads.

Threads

C++ algorithms that can take an execution policy

  1. std::sort
  2. std::copy
  3. std::transform
  4. std::accumulate
  5. std::for_each
  6. std::reduce
  7. std::inclusive_scan
  8. std::exclusive_scan
  9. std::transform_reduce
  10. std::replace_if
  11. std::replace
  12. std::remove_if
  13. std::remove
  14. std::count_if
  15. std::count
  16. std::max_element
  17. std::min_element
  18. std::find_if
  19. std::find
  20. std::generate

References