The documentation says it is deprecated. What's the system semaphore? And what's the best replacement for this struct now?
Deprecated since 1.7.0: easily confused with system semaphore and not used enough to pull its weight
The documentation says it is deprecated. What's the system semaphore? And what's the best replacement for this struct now?
Deprecated since 1.7.0: easily confused with system semaphore and not used enough to pull its weight
System semaphore refers to whatever semaphore the operating system provides. On POSIX (Linux, MacOS) these are the methods you get from #include <semaphore.h> (man page). std::sync::Semaphore is implemented only in rust and does not involve the OS's synchronization primitives.
If you need a binary semaphore (i.e. a mutex) You can use std::sync::Mutex. std::sync::RwLock is similar, but designed to solve the readers–writers problem. For something akin to a general semaphore, you could use an AtomicIsize and a busy loop, but the busy loop will hamper performance.
std::sync::Semaphore was never stabilized. I managed to miss this fact the first time around because the 'unstable' warning box was not displayed, probably because the 'deprecated' box replaced it. The source code for Semaphore contains an unstable attribute
#![unstable(feature = "semaphore",
reason = "the interaction between semaphores and the acquisition/release \
of resources is currently unclear",
issue = "27798")]
The issue number in the header specifies the discussion about this feature.