AdeleGoldberg
AdeleGoldberg

Reputation: 1349

Updating an atomic variable with a non atomic and vice versa

I am updating an atomic variable size_t using from one thread and reading it from another. Following is the code:

Code:

// MyClass.hpp
#pragma once
#include <atomic>

class MyClass {
public:
    size_t GetVal() {
        return m_atomic_val;
    }

    void SetVal(const std::size_t val) {
        m_atomic_val = val;
    }

private:
    std::atomic<size_t> m_atomic_val{0};
};


// main.cpp
#include "MyClass.hpp"

#include <iostream>
#include <thread>

int main() {
    MyClass obj;
    obj.SetVal(4);

    std::thread my_thread = std::thread([&obj]{
       std::cout << "Thread started" << std::endl;
       std::this_thread::sleep_for(std::chrono::milliseconds(30));
        obj.SetVal(8);
    });

   std::this_thread::sleep_for(std::chrono::seconds(2));
   auto val = obj.GetVal();
   std::cout << "The value is: " << val << std::endl;
   my_thread.join();
}

Question:

Environment:
My code runs on iOS, Android and macOS.

Upvotes: 0

Views: 1574

Answers (1)

Some programmer dude
Some programmer dude

Reputation: 409442

Assignment to an atomic object will accept any value of the correct type.

Since you have an std::atomic<size_t> then any value or variable that is (or can be implicitly converted to) a size_t can be used on the right-hand side of the assignment.

As for the GetVal issue, std::atomic have a conversion operator which will do the right thing, atomically fetch the value and give it to you for the return.

Another note regarding the GetVal function: It will only be the fetching of the value that will be atomic, the return of it will not be. So between the fetch and the actual return the value of m_atomic_val can change, but the old value will be returned.

Upvotes: 2

Related Questions