Direct use of a futex?


on Linux, I need to have a futex from a mmap address (acting like a database) to avoid multiple writers.

What is the most efficient way to achieve this ?

The futex implementation seems to require a ::new(X) call;

Instead I should be able to read directly from the mmap file database field that value and wait on it if not the appropriate value.
Especially this part is of interest:

           futex1 = &iaddr[0];

of the main function in the example located in the futex man page


FYI that's an internal implementation in the stdlib, it is not exposed to the public.

Are you referring to this part?

    pub const fn new() -> Self {
        Self { futex: AtomicU32::new(0) }

This is done here because a mutex owns its internal futex. Maybe you're interested in the underlying futex_wait and futex_wake operations defined in ?

Thanks SkiFire13, this is EXACTLY what I was after.
You confirm that it is a public API I can freely use?

The page I linked is also not part of the public API. It is however pretty small and mostly uses the libc crate which instead is public API, so you could take it as an example of what you need to do. The libc crate is mostly an interface to the C APIs, so the man page documentation you previously linked should also apply to it. The downside is that it is very unsafe to use.

I will deal with the unsafe and use the libc crate.

Thanks a lot :grinning: :+1:

Have you considered the atomic-wait crate?

That's a really neat crate I overlooked.
I've already created the C++->C->Rust binding, so the DB is still C++ and we will gradually move over Rust at a later stage.
But this is the crate we will be using for sure for future development.

Thanks! :+1:

I read the source code of this crate and would just warn users that the futex set the FUTEX_PRIVATE_FLAG so it is specific to a local process as the man explains

Just a note.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.