Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit e77b474

Browse files
authoredJun 16, 2024
Update Arc::try_unwrap() docs
Clarify the language wrt `race condition` not meaning `memory unsafety`.
1 parent ae7f43e commit e77b474

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed
 

‎alloc/src/sync.rs

+12-9
Original file line numberDiff line numberDiff line change
@@ -940,15 +940,18 @@ impl<T, A: Allocator> Arc<T, A> {
940940
/// This will succeed even if there are outstanding weak references.
941941
///
942942
/// It is strongly recommended to use [`Arc::into_inner`] instead if you don't
943-
/// want to keep the `Arc` in the [`Err`] case.
944-
/// Immediately dropping the [`Err`] payload, like in the expression
945-
/// `Arc::try_unwrap(this).ok()`, can still cause the strong count to
946-
/// drop to zero and the inner value of the `Arc` to be dropped:
947-
/// For instance if two threads each execute this expression in parallel, then
948-
/// there is a race condition. The threads could first both check whether they
949-
/// have the last clone of their `Arc` via `Arc::try_unwrap`, and then
950-
/// both drop their `Arc` in the call to [`ok`][`Result::ok`],
951-
/// taking the strong count from two down to zero.
943+
/// keep the `Arc` in the [`Err`] case.
944+
/// Immediately dropping the [`Err`]-value, as the expression
945+
/// `Arc::try_unwrap(this).ok()` does, can cause the strong count to
946+
/// drop to zero and the inner value of the `Arc` to be dropped.
947+
/// For instance, if two threads execute such an expression in parallel,
948+
/// there is a race condition without the possibility of unsafety:
949+
/// The threads could first both check whether they own the last instance
950+
/// in `Arc::try_unwrap`, determine that they both do not, and then both
951+
/// discard and drop their instance in the call to [`ok`][`Result::ok`].
952+
/// In this scenario, the value inside the `Arc` is safely destroyed
953+
/// by exactly one of the threads, but neither thread will ever be able
954+
/// to use the value.
952955
///
953956
/// # Examples
954957
///

0 commit comments

Comments
 (0)
Failed to load comments.