Home > Backend Development > C++ > body text

Why Doesn\'t the C Standard Include a Non-Atomic `std::shared_ptr`?

Mary-Kate Olsen
Release: 2024-10-30 02:03:28
Original
780 people have browsed it

Why Doesn't the C   Standard Include a Non-Atomic `std::shared_ptr`?

Non-Atomic Smart Pointers: Exploring the Absence in the C Standard

In the realm of C memory management, the std::shared_ptr stands as an exceptional smart pointer due to its atomic operations. This raises the question: why isn't there a non-atomic counterpart within the standard memory library?

1. Non-Atomic Alternatives to std::shared_ptr

While the std::shared_ptr offers atomic behavior, some scenarios may not warrant such overhead. To address this, external libraries have emerged, providing non-atomic reference-counted smart pointers. However, the standard memory library itself lacks such offerings.

2. The Absence in C 11: Reasons and Debate

Despite the perceived utility, a proposition for a non-atomic version of std::shared_ptr was rejected during the C 11 standardization process. Several arguments fueled this decision:

  • Potential for misuse: Non-atomic smart pointers could be inadvertently used in multithreaded contexts, leading to difficult-to-debug errors.
  • Consistency and interoperability: Maintaining a single "universal" shared_ptr with atomic functionality allowed for smoother interoperability between C libraries, including third-party ones.
  • Performance considerations: While atomic operations incur a performance cost, they are not prohibitively expensive, especially when move construction and move assignment are utilized.
  • Customization options: If a programmer requires non-atomic behavior, they are not prevented from creating their own custom reference-counted smart pointer implementation.

Conclusion

The absence of a non-atomic std::shared_ptr in the C standard is the result of a deliberate decision based on concerns about potential misuse, consistency, performance, and customization flexibility. While external libraries can provide such alternatives, the standard itself has prioritized the benefits of a universal, atomic smart pointer for memory management.

The above is the detailed content of Why Doesn\'t the C Standard Include a Non-Atomic `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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!