rust/library/std/src/sync
Guillaume Gomez ee04e0f35e
Rollup merge of #125696 - workingjubilee:please-dont-say-you-are-lazy, r=Nilstrieb
Explain differences between `{Once,Lazy}{Cell,Lock}` types

The question of "which once-ish cell-ish type should I use?" has been raised multiple times, and is especially important now that we have stabilized the `LazyCell` and `LazyLock` types. The answer for the `Lazy*` types is that you would be better off using them if you want to use what is by far the most common pattern: initialize it with a single nullary function that you would call at every `get_or_init` site. For everything else there's the `Once*` types.

"For everything else" is a somewhat weak motivation, as it only describes by negation. While contrasting them is inevitable, I feel positive motivations are more understandable. For this, I now offer a distinct example that helps explain why `OnceLock` can be useful, despite `LazyLock` existing: you can do some cool stuff with it that `LazyLock` simply can't support due to its mere definition.

The pair of `std::sync::*Lock`s are usable inside a `static`, and can serve roles in async or multithreaded (or asynchronously multithreaded) programs that `*Cell`s cannot. Because of this, they received most of my attention.

Fixes #124696
Fixes #125615
2024-06-04 21:41:34 +02:00
..
barrier std: move "mod tests/benches" to separate files 2020-08-31 02:56:59 +00:00
condvar SeqCst->Relaxed in condvar test. 2024-03-20 15:38:09 +01:00
lazy_lock More inference-friendly API for lazy 2022-10-29 09:56:20 +01:00
mpmc fix typos 2024-02-29 01:33:02 -05:00
mpsc remove redundant imports 2023-12-10 10:56:22 +08:00
mutex Specify behavior if the closure passed to *Guard::*map panics. 2023-12-05 17:30:46 -06:00
once Stabilize poison API of Once, rename poisoned() 2021-02-04 15:20:14 +01:00
once_lock Spelling library/ 2023-04-26 02:10:22 -04:00
reentrant_lock remove an unused type from the reentrant lock tests 2024-04-22 19:36:21 +02:00
rwlock Specify behavior if the closure passed to *Guard::*map panics. 2023-12-05 17:30:46 -06:00
barrier.rs branch 1.78: replace-version-placeholder 2024-03-19 19:27:24 -04:00
condvar.rs std: move Once implementations to sys 2024-03-12 15:41:06 +01:00
lazy_lock.rs Move first OnceLock example to LazyLock 2024-06-02 22:53:41 -07:00
mod.rs Differ LazyLock vs. OnceLock in std::sync overview 2024-06-02 22:53:41 -07:00
mutex.rs docs(sync): normalize dot in fn summaries 2024-03-22 23:04:20 +01:00
once_lock.rs Rollup merge of #125696 - workingjubilee:please-dont-say-you-are-lazy, r=Nilstrieb 2024-06-04 21:41:34 +02:00
once.rs std: move Once implementations to sys 2024-03-12 15:41:06 +01:00
poison.rs Use min_exhaustive_patterns in core & std 2024-03-12 08:20:46 +01:00
reentrant_lock.rs Add manual Sync impl for ReentrantLockGuard 2024-05-24 17:44:37 -07:00
rwlock.rs Update RwLock deadlock example to not use shadowing 2024-03-26 21:40:31 -04:00