Home > Backend Development > C++ > body text

How to implement thread safety in C++ class design?

WBOY
Release: 2024-06-03 18:48:09
Original
1005 people have browsed it

In order to achieve thread safety, there are two methods in C++: use a mutex to protect the critical section, allowing only one thread to access it at a time. Using atomic operations, operations are performed in an indivisible manner, eliminating concurrent access issues.

How to implement thread safety in C++ class design?

Achieving thread safety in C++ class design

Introduction

In many In a threaded environment, it is crucial to ensure the thread safety of data. There are several ways to achieve this in C++. This article will explore how to use mutexes and atomic operations to design thread-safe classes.

Mutex

A mutex is a locking mechanism that allows only one thread to access the critical section at a time. When a thread enters the critical section, it acquires ownership of the mutex. Other threads trying to enter the critical section will block until the thread releases the mutex.

class ThreadSafeCounter {
private:
    std::mutex m_mutex;
    int m_count;

public:
    void increment() {
        std::lock_guard<std::mutex> lock(m_mutex);
        ++m_count;
    }

    int get() {
        std::lock_guard<std::mutex> lock(m_mutex);
        return m_count;
    }
};
Copy after login

In the above example, both the increment() and get() methods are protected using std::mutex from the standard library critical section. While one thread is updating the count, other threads cannot enter the increment() method at the same time.

Atomic Operations

Atomic operations are a special type of operation that are performed in an indivisible manner. This means that these operations can only be performed in a single thread at a time, eliminating problems caused by concurrent access. The std::atomic library was introduced in C++11, which provides classes for atomic operations.

class ThreadSafeCounterAtomic {
private:
    std::atomic<int> m_count;

public:
    void increment() {
        ++m_count;
    }

    int get() {
        return m_count.load();
    }
};
Copy after login

In this case, m_count is an atomic integer that can be safely incremented and retrieved from multiple threads. std::atomic<int>::load() Method obtains the value of an atomic integer in a thread-safe manner.

Practical Case

Consider an example of a shared counter that needs to be updated in parallel from multiple threads:

#include <thread>

int main() {
    std::unique_ptr<ThreadSafeCounter> counter = std::make_unique<ThreadSafeCounter>();

    std::vector<std::thread> threads(10);
    for (auto& thread : threads) {
        thread = std::thread([&] {
            for (int i = 0; i < 1000000; ++i) {
                counter->increment();
            }
        });
    }

    for (auto& thread : threads) {
        thread.join();
    }

    std::cout << "最终计数:" << counter->get() << std::endl;
}
Copy after login

In this program, we start from 10 Threads update the counter in parallel, then print the final count in the main thread. The mutex ensures that the counter can only be updated by at most one thread at any time, thus ensuring thread safety.

Conclusion

Thread-safe C++ classes can be designed by using mutexes and atomic operations. Mutexes are suitable for protecting critical sections that require serial access, while atomic operations are suitable for operations that do not require serial access and can be performed atomically.

The above is the detailed content of How to implement thread safety in C++ class design?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!