Concurrency

1

Concurrency: Send, Sync, Arc, and Mutex

6 minute

In C, the basic threading tools are direct:

pthread_create(&tid, NULL, worker, ctx);
pthread_mutex_lock(&lock);
/* modify shared state */
pthread_mutex_unlock(&lock);

These tools can build reliable systems, but many rules are conventions:

  • whether ctx points to data that may cross threads
  • who owns that data
  • whether multiple threads read and write it at the same time
  • whether locking was forgotten
  • which fields are protected by which lock
  • whether resources outlive callbacks and thread exit paths

Rust does not make concurrency automatically simple. It moves some thread-safety requirements into types:

Read More