pub fn with_critical_section2<F, R>(
a: &Bound<'_, PyAny>,
b: &Bound<'_, PyAny>,
f: F,
) -> Rwhere
F: FnOnce() -> R,
Expand description
Executes a closure with a Python critical section held on two objects.
Acquires the per-object lock for the objects a
and b
that are held
until the closure f
is finished.
This is structurally equivalent to the use of the paired Py_BEGIN_CRITICAL_SECTION2 and Py_END_CRITICAL_SECTION2 C-API macros.
A no-op on GIL-enabled builds, where the critical section API is exposed as a no-op by the Python C API.
Provides weaker locking guarantees than traditional locks, but can in some cases be used to provide guarantees similar to the GIL without the risk of deadlocks associated with traditional locks.
Many CPython C API functions do not acquire the per-object lock on objects passed to Python. You should not expect critical sections applied to built-in types to prevent concurrent modification. This API is most useful for user-defined types with full control over how the internal state for the type is managed.