Home > Backend Development > C++ > Why Doesn\'t C \'s Standard Library Offer a Non-Atomic Version of `std::shared_ptr`?

Why Doesn\'t C \'s Standard Library Offer a Non-Atomic Version of `std::shared_ptr`?

Patricia Arquette
Release: 2024-10-27 00:03:02
Original
255 people have browsed it

Why Doesn't C  's Standard Library Offer a Non-Atomic Version of `std::shared_ptr`?

The Non-Atomic Equivalent of std::shared_ptr

Question:

Is there an alternative to std::shared_ptr that lacks its inherent atomicity? Moreover, why does the C Standard Library not include a non-atomic option?

Answer:

1. Availability of Non-Atomic Alternatives:

The C Standard Library does not offer a non-atomic variant of std::shared_ptr. However, third-party libraries may provide such implementations. Prior to the introduction of std::shared_ptr in C 11, many developers created their own reference-counted smart pointers, potentially offering non-atomic options.

2. Rationale for Atomic std::shared_ptr in C 11:

During the C 11 standardization process, the proposal to include a non-atomic version of std::shared_ptr was considered and ultimately rejected. The following arguments influenced this decision:

  • Potential Misuse: Non-atomic smart pointers written in threaded code could introduce subtle and difficult-to-debug errors.
  • Consistency and Interoperability: Having a single, consistent version of std::shared_ptr facilitates library interoperability and reduces confusion.
  • Low Performance Cost: Atomic operations have a non-zero overhead, but it is mitigated by the use of move semantics, which avoid atomic operations during common operations like vector erasure and insertion.
  • Custom Implementations: Developers can create their own non-atomic smart pointers if they desire.

Proposed Non-Atomic Version:

Despite the rejection in C 11, there may have been proposals for a non-atomic version of std::shared_ptr submitted to Boost or the standards committee. However, these proposals did not gain sufficient support for inclusion in the official standard.

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

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template