How 'undefined' a race condition can be?

前端 未结 7 463
甜味超标
甜味超标 2021-01-03 02:49

Let\'s say I define a following C++ object:

class AClass
{
public:
    AClass() : foo(0) {}
    uint32_t getFoo() { return foo; }
    void changeFoo() { foo          


        
相关标签:
7条回答
  • 2021-01-03 03:24

    Not sure what you're looking for. On most modern architectures, there is a very distinct possibility that getFoo() always returns 0, even after changeFoo has been called. With just about any decent compiler, it's almost guaranteed that getFoo(), will always return the same value, regardless of any calls to changeFoo, if it is called in a tight loop.

    Of course, in any real program, there will be other reads and writes, which will be totally unsynchronized with regards to the changes in foo.

    And finally, there are 16 bit processors, and there may also be a possibility with some compilers that the uint32_t isn't aligned, so that the accesses won't be atomic. (Of course, you're only changing bits in one of the bytes, so this might not be an issue.)

    0 讨论(0)
提交回复
热议问题