Skip to the content ForoHackers
Log inJoin

Concurrency and parallelism: start here

Concurrency and parallelism
#3043 · · Quote · Report

Concurrency is structuring a program as several activities that make progress in overlapping time; parallelism is running them at the same instant on several cores. Both are needed to use modern hardware and to handle many things at once, and both bring bugs that appear only sometimes and are hard to reproduce.

Key concepts

  • Threads and processes: units the operating system schedules, with shared or separate memory.
  • Shared state and locks: mutexes protect data, but they invite deadlock and contention.
  • Message passing: activities share nothing and talk through channels or mailboxes.
  • Async and event loops: one thread juggles many waiting tasks with callbacks, futures or coroutines.
  • Memory model and atomics: what one thread may see of another's writes, and the operations that make it safe.
  • Data parallelism: split the same work across cores, then combine the results.

Vocabulary

  • Race condition: a result that depends on the timing of unsynchronised accesses.
  • Deadlock: tasks each waiting for something another holds, so none can move.
  • Atomic: an operation that appears to happen all at once.
  • Amdahl's law: the serial part of a task limits the speed-up from adding cores.

What to study, in order

  1. Start two threads that increment one counter, watch it go wrong, then fix it with a lock.
  2. Build a producer and consumer with a queue, then again with channels.
  3. Cause a deadlock on purpose, then fix it with a consistent lock order.
  4. Write the same server with threads and with an event loop; compare behaviour under load.
  5. Read your language's memory model and learn what an atomic guarantees.
  6. Use a race detector or thread sanitiser on your own code.

Related tags

#distributed-systems #rtos #go #erlang

You are not logged in, so this goes up anonymously: no account, no name and no address is stored with it, and it may carry no link and no image. Log in or join to post under your name. Our web server's logs keep the address and time of every request for up to 14 days; to keep yours out of them, use Tor. To reach the staff in confidence, use the drop. Sign as a returning source (a key kept only in this browser)

Sending takes a few seconds here: your browser first works out a proof of work against spam.

Reply