![]() There are different implications depending on whether the integer is in a positive or negative state. The term atomic means that even when multiple threads are all attempting to change the value of the semaphore, those changes occur in a sequential (and thus deterministic) order. You can imagine that the integer represents some number of resources that only a certain number of threads are able to access or acquire at the same time. Semaphores are one of the simplest synchronization primitives upon which more complex tools (such as mutexes, barriers, etc.) can be built upon.Ĭonceptually, a semaphore is just an integer that can only be incremented and decremented in an atomic fashion. Maybe in the future, this will be something I’ll revisit! I’m sure there’s a more low-level way to do this in Rust that better captures the primitive nature of these primitives, but that likely involves writing unsafe code and I’m just not comfortable enough yet with that. Similarly, for this semaphore implementation we’ll be using standard library types that are “more complex” from a conceptual and implementation standpoint than the primitive we’re actually implementing. It’s similar to implementing a hash map in JavaScript: since everything in JS boils down to an Object, you’re essentially implementing a hash map using hash maps! Implementing synchronization primitives in Rust in a safe fashion is a bit strange in a circular kind of way. I hope others find this exploration interesting, insightful, and/or helpful! □ ![]() Naturally, reading through the book inspired me to implement these synchronization primitives and problems in Rust. I recently stumbled upon Allen Downey’s The Little Book of Semaphores, which is a short, sweet, and to-the-point textbook on the topic of synchronization.
0 Comments
Leave a Reply. |