7
Using OnceCell with Regex (for reading only)
(lemmyrs.org)
Ask any question relevant to the Rust language and its ecosystem. No such questions shall ever be considered dumb!
Ask for contributions for your crate
So, the error you're getting here is that the
staticitself needs synchronization, multiple threads can try to initialize theOnceCellat once because this is a static (only one initialization function gets called, the rest have to block).consider an example like this:
lazy_staticuses a lock under the hood. Specifically, whenstdis available (when std isn't available I think it uses a spinlock but I didn't actually check) it uses https://doc.rust-lang.org/std/sync/struct.Once.html (not to be confused withOnceCell/OnceLock), which explicitly states it locks. As in the excerpt below:The short answer is, yes, you have to use
OnceLockif you want this in a static, andOnceLockdoes roughly whatlazy_staticdoes anyway.First of all, thank you for this very elaborate answer. So that means OnceCell can never be used in a static but only as local variable / member variable? I see, that makes sense and also reduces its use cases by a lot of what I initially thought. But your example makes sense and the name/package also suggest it's not thread-safe in itself, I had just wrongly assumed OnceCell was meant to be used in static contexts.
Thanks!
Yeah, it doesn't help that in the
once_cellcrate, the thread-safe version was also calledOnceCell; you had to choose betweenonce_cell::sync::OnceCellandonce_cell::unsync::OnceCell. But when it was added to the standard library,once_cell::sync::OnceCellwas renamed toOnceLockto make them easier distinguishable. So