2

I'm implementing a spin lock in C++ (since for whatever reason there's no spin lock implementation in the standard library) and I'm looking for a cross-platform and cross-architecture way to emit a PAUSE/YIELD instruction.
Rust has core::hint::spin_loop for that. Is there something similar for C++?

eyelash
  • 3,197
  • 25
  • 33
  • you can probably peek into e.g. boost library internals: [spinlock](https://www.boost.org/doc/libs/1_76_0/boost/smart_ptr/detail/spinlock_std_atomic.hpp), [yield](https://www.boost.org/doc/libs/1_76_0/boost/smart_ptr/detail/yield_k.hpp) and so on. – dewaffled Jul 10 '21 at 10:13
  • 1
    Have a look at Google's absl, Facebook's folly, they are production level libraries that have spinlock and other similar but more powful utils. – prehistoricpenguin Jul 10 '21 at 10:15
  • 1
    @dewaffled [the boost implementation of `sp_thread_pause`](https://www.boost.org/doc/libs/1_76_0/boost/smart_ptr/detail/sp_thread_pause.hpp) unfortunately only has an x86 implementation. – eyelash Jul 10 '21 at 10:22
  • @eyelash What implementation do you need? Rust [implements](https://doc.rust-lang.org/src/core/hint.rs.html#110-140) only x86(-64) and ARM(64). – Daniel Kleinstein Jul 10 '21 at 10:39
  • @DanielKleinstein at least x86 and ARM but preferably something future-proof that I don't need to manually update in the future in order to support more architectures. – eyelash Jul 10 '21 at 11:22
  • it seems folly indeed has more architectures covered in [asm_volatile_pause](https://github.com/facebook/folly/blob/master/folly/portability/Asm.h#L36) – dewaffled Jul 10 '21 at 13:27

1 Answers1

1

Maybe something like

std::this_thread::yield();

?

PeppeDx
  • 133
  • 1
  • 10