Home >Backend Development >C++ >Why Doesn\'t C Have a Non-Atomic `std::shared_ptr`?

Why Doesn\'t C Have a Non-Atomic `std::shared_ptr`?

Linda Hamilton
Linda HamiltonOriginal
2024-11-01 01:21:02858browse

 Why Doesn't C   Have a Non-Atomic `std::shared_ptr`?

Non-Atomic Equivalent of std::shared_ptr and Why It's Absent in

std::shared_ptr has gained prominence as the atomically incrementing and decrementing shared ownership smart pointer. However, the question arises of whether a non-atomic equivalent exists.

1. Non-Atomic Alternative Options

Despite extensive examination of the header, no non-atomic equivalent of std::shared_ptr is readily accessible. However, there are solutions outside the standard.

2. Why a Non-Atomic std::shared_ptr Was Not Included in C 11

The absence of a non-atomic version in C 11 stems from discussions held during the Rapperswil meeting in 2010.

Reasons Against Non-Atomic Option:

  • Potential issues when code written without atomicity is later used with threads, leading to debugging challenges.
  • The advantage of having a universal shared_ptr with consistent behavior across libraries.
  • The minimal performance impact of atomicity, which is mitigated by efficient move construction and move assignment operations.
  • The availability of custom non-atomic shared pointers if required.

Based on these arguments, the Language Working Group (LWG) decided against introducing a non-atomic variant of shared_ptr in C 11.

The above is the detailed content of Why Doesn\'t C Have a Non-Atomic `std::shared_ptr`?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn