Does esp32 rust std support std::sync::Mutex?

Hi,

Wanted to know if rust on esp32 (Xtensa) supports std Mutex ? If not are there any other crates like critical_section or so that has a Mutex that will work on esp32 ?

Rgds,
Gopa.

1 Like

I'm not familiar with esp32, but for embedded targets in general, the std crate is unavailable due to the lack of underlying operating system services. specifically, std::sync::Mutex needs to use system calls to put the current thread to sleep when the mutex cannot be locked.

critical-section::Mutex is commonly used for embedded platforms, there's also the bare-metal crate, but it is superseded by critical-section in newer (but unpublished on crates.io) versions. be careful to choose the correct critical section implementation (typically provided by the rt crates for the target), as single-core and multi-core have different requirements.

note however, critical-section::Mutex has different interface from std::sync::Mutex, you probably want to use critical-section::Mutex<RefCell<T>>, and that's intentional API design, see the doc:

Rust for esp32 exists in two variants: std (built on top of ESP-IDF which uses FreeRTOS) and no-std (bare metal).

I'm pretty sure the former supports std::sync::mutex. The latter likely need critical section.

I haven't tried the no-std approach, and have only briefly played with the std variant (didn't do anything threaded).

1 Like

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.