Chapter 10. Ideas and Inspiration

There are an infinite number of concurrency related topics, algorithms, data structures, anecdotes, and other potential chapters that could be part of this book. However, we’ve arrived at the final chapter and it’s almost time for us to part ways, hopefully leaving you with an excited feeling of new possibilities and ready to apply new knowledge and skills in practice.

This final chapter’s purpose is to provide inspiration for your own creations and future work by showing you some ideas that you can study, explore, and build on your own.

Semaphore

A semaphore is effectively just a counter with two operations: signal (also called up or V) and wait (also called down or P). The signal operation increments the counter up to a certain maximum, while a wait operation decrements the counter. If the counter is zero, a wait operation will block and wait for a matching signal operation, preventing the counter from ever becoming negative. It is a flexible tool that can be used to implement other synchronization primitives.

Visualization of a semaphore. A seemingly infinite strip with integers starting at zero, with the word 'blocked' where negative numbers would be. A large arrow is currently pointing at zero, but can move left and right through the wait and signal operations.

A semaphore can be implemented as a combination of a Mutex<u32> for the counter and a Condvar for wait operations to wait for. However, there are several ways to implement it more efficiently. Most notably, on platforms that support futex-like operations (“Futex”), it can be implemented more efficiently as a single AtomicU32 (or even ...

Get Rust Atomics and Locks now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.