1

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

Yuchen
  • 27,526
  • 21
  • 148
  • 215

1 Answers1

6

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.

trent
  • 21,712
  • 7
  • 47
  • 79
asky
  • 1,250
  • 10
  • 20
  • 1
    it looks like Semaphore is already removed. No longer in version 1.40 at least based on https://github.com/rust-lang/rust/issues/67606#issuecomment-568914561 – Yuchen Dec 26 '19 at 20:01
  • 1
    Sorry, I was mistaken - semaphore was never "stabalized" in the first place - it was only available on a nightly compiler, never the stable one. Breaking changes are allowed to happen in the nightly compiler, just not the stable one. I have now edited my original answer. – asky Dec 26 '19 at 20:58