Is a Critical Section around an integer getter and setter redundant?

Posted by Tim Gradwell on Stack Overflow See other posts from Stack Overflow or by Tim Gradwell
Published on 2010-06-17T15:00:57Z Indexed on 2010/06/17 15:03 UTC
Read the original article Hit count: 130

Filed under:
|

Do critical sections inside trivial int accessors actually do anything useful?

int GetFoo()
{
   CriticalSection(crit_id);
   return foo;
}

void SetFoo(int value)
{
   CriticalSection(crit_id);
   foo = value;
}

Is it possible for two threads to be attempting to read and write foo simultaneously? I'd have thought 'no' unless integers are written byte-at-a-time, in which case I can see the use. But I'd have though modern cpus would read/write integers in a single atomic action...

© Stack Overflow or respective owner

Related posts about c++

Related posts about multithreading