Does defining a std:atomic<bool> variable inside class influence its atomicity?

Viewed 56

I am kind of new to use std:atomic. I wonder to ask if I can put a std::atomic variable inside a class and define setter and getter for it.

For example if i define an extern instance of the following class as a global variable. Is it thread safe to set it in file1 and read it in file2, while they are running in different threads?

Is there any tricky bug in it? Is it thread-safe?

class TEST {
public:
    TEST() {
        test_ = false;
    }

    bool get() {
        bool test_r = test_.load(std::memory_order_acquire);
        return test_r;
    }
    void set(bool v) {
        test_.store(v, std::memory_order_release);
    }

 private:
    std::atomic<bool> test_;
};

//defined in global.h
namespace glob {
  extern TEST g_test;
}

//set in file1.cc
 glob::g_test.set(true);


// get in file2.cc
 bool b_read  = glob::g_test.get();         
0 Answers
Related