c++ criticalsection for getter

Viewed 936

I have a simple class with one private member that is accessible via get() and set() in a multithreaded environment (multi readers/multi writers). how do I lock a Get() as it only has a return statement?

class MyValue
{
  private:
    System::CriticalSection lock;
    int val { 0 };

  public:
    int SetValue(int arg)
    {
        lock.Enter();
        val = arg;
        lock.Leave();
    }

    int GetValue()
    {
        lock.Enter();
        return val;
        //Where should I do lock.Leave()?
    }   
}
4 Answers
Related