Do I need a memory barrier for a change notification flag between threads?

前端 未结 3 2013
一生所求
一生所求 2021-02-06 11:54

I need a very fast (in the sense \"low cost for reader\", not \"low latency\") change notification mechanism between threads in order to update a read cache:

The

3条回答
  •  隐瞒了意图╮
    2021-02-06 12:36

    Do I need to qualify updateAvailable as volatile?

    As volatile doesn't correlate with threading model in C++, you should use atomics for make your program strictly standard-confirmant:

    On C++11 or newer preferable way is to use atomic with memory_order_relaxed store/load:

    atomic updateAvailable;
    
    //Writer
    ....
    updateAvailable.store(true, std::memory_order_relaxed); //set (under mutex locked)
    
    // Reader
    
    if(updateAvailable.load(std::memory_order_relaxed)) // check
    {
        ...
        updateAvailable.store(false, std::memory_order_relaxed); // clear (under mutex locked)
        ....
    }
    

    gcc since 4.7 supports similar functionality with in its atomic builtins.

    As for gcc 4.6, it seems there is not strictly-confirmant way to evade fences when access updateAvailable variable. Actually, memory fence is usually much faster than 10-100ms order of time. So you can use its own atomic builtins:

    int updateAvailable = 0;
    
    //Writer
    ...
    __sync_fetch_and_or(&updateAvailable, 1); // set to non-zero
    ....
    
    //Reader
    if(__sync_fetch_and_and(&updateAvailable, 1)) // check, but never change
    {
        ...
        __sync_fetch_and_and(&updateAvailable, 0); // clear
        ...
    }
    

    Is it safe regarding data consistency?

    Yes, it is safe. Your reason is absolutely correct here:

    the shared resource is never touched/read in the Reader fast path.


    This is NOT double-check locking!

    It is explicitely stated in the question itself.

    In case when updateAvailable is false, Reader thread uses variable myDataCache which is local to the thread (no other threads use it). With double-check locking scheme all threads use shared object directly.

    Why memory fences/barriers are NOT NEEDED here

    The only variable, accessed concurrently, is updateAvailable. myData variable is accessed with mutex protection, which provides all needed fences. myDataCache is local to the Reader thread.

    When Reader thread sees updateAvailable variable to be false, it uses myDataCache variable, which is changed by the thread itself. Program order garantees correct visibility of changes in that case.

    As for visibility garantees for variable updateAvailable, C++11 standard provide such garantees for atomic variable even without fences. 29.3 p13 says:

    Implementations should make atomic stores visible to atomic loads within a reasonable amount of time.

    Jonathan Wakely has confirmed, that this paragraph is applied even to memory_order_relaxed accesses in chat.

提交回复
热议问题