0:00
/
0:00
Transcript

The Most Frustrating Problem in Rust

Referring to persistent entities in multiple threads

Code: https://github.com/caveofprogramming/rust/

The most frustrating thing about Rust that I’ve found so far is the problem of how to refer to persistent entities that have to maintain their own state, across multiple threads.

In this video I’ll show you how to tackle the problem using Arc and Mutex.

However, this isn’t suitable for every situation.

For example, if your persistent entity happens to manage a collection of software instruments which have to be modified from a particular thread, while your audio thread separately has to read audio samples from those instruments, and the persistent entity must remain modifiable—then probably Mutexes aren’t the answer.

Locking mutexes in audio threads, or real-time threads generally, is widely considered a bad idea.

The best way I’ve found so far of dealing with this, after trying out a bunch of different things, is to use channels, which we covered earlier. These may not be fast enough to send one audio sample at a time, but we can send small blocks of data (maybe 512 samples) at a time, and recycle the blocks to and from the producer and consumer threads. That’s a topic for a future post!

Discussion about this video

User's avatar

Ready for more?