Home > Backend Development > C++ > Does `const` in C 11 Guarantee Thread Safety?

Does `const` in C 11 Guarantee Thread Safety?

DDD
Release: 2024-12-20 20:59:10
Original
859 people have browsed it

Does `const` in C  11 Guarantee Thread Safety?

Does 'const' Imply Thread Safety in C 11?

Introduction

It is widely believed that employing 'const' in C 11 guarantees thread safety. However, this notion requires further clarification.

Clarifying the Claim

While 'const' alone does not guarantee thread safety, it fulfills the Standard Library's expectation that operations on 'const' objects are thread-safe. Specifically:

  • Operations marked 'const' should entirely consist of reads (no writes) or internally synchronize writes.
  • The Standard Library assumes any operations on 'const' objects within itself to be non-racing (as long as its non-const arguments handle concurrency).
  • If a type's operations on its 'const' objects violate this expectation, using that type with the Standard Library could result in data races and undefined behavior.

Const Does Not Equal Java's Synchronized

Unlike Java's 'synchronized,' 'const' does not inherently provide synchronization. Consider the following example:

class rect {
    int width = 0, height = 0;

public:
    void set_size(int new_width, int new_height) {
        width = new_width;
        height = new_height;
    }
    int area() const {
        return width * height;
    }
};
Copy after login
  • The 'area()' function is thread-safe because it only reads, not writes.
  • However, 'rect' itself is not thread-safe since it does not synchronize the write operations performed by 'set_size()'.

Conditional Thread Safety with 'const'

To properly utilize 'const' for thread safety with writes, mutable state (like a cached area value) must be protected by synchronization primitives, as demonstrated below:

class rect {
    int width = 0, height = 0;

    mutable std::mutex cache_mutex;
    mutable int cached_area = 0;
    mutable bool cached_area_valid = true;

public:
    void set_size(int new_width, int new_height) {
        if (new_width != width || new_height != height) {
            std::lock_guard< std::mutex > guard(cache_mutex);
            cached_area_valid = false;
        }
        width = new_width;
        height = new_height;
    }
    int area() const {
        std::lock_guard< std::mutex > guard(cache_mutex);
        if (!cached_area_valid) {
            cached_area = width * height;
            cached_area_valid = true;
        }
        return cached_area;
    }
};
Copy after login

Despite 'area()' being thread-safe, 'rect' still remains non-thread-safe due to unprotected writes in 'set_size()'.

Keyword Shortage

The claim that C developers are running out of keywords is true, as the language has had a limited number of reserved words since its inception.

The above is the detailed content of Does `const` in C 11 Guarantee Thread Safety?. 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template