microsoft / microsoft/STL

<atomic>: Does atomic_thread_fence need compiler barriers when using _InterlockedIncrement?

Open
#1,791 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

question
Dominant language
C++
Stars
11.1k
Forks
1.7k
Avg merge
4d 15h
Merged PRs (30d)
22

Description

I thought compiler barriers were implied for interlocked functions, but it doesn't look that way in atomic_thread_fence:
https://github.com/microsoft/STL/blob/62137922ab168f8e23ec1a95c946821e24bde230/stl/inc/atomic#L267

Instead of this:

_Compiler_barrier();
if (_Order == memory_order_seq_cst) {
    volatile long _Guard;
    (void) _InterlockedIncrement(&_Guard);
    _Compiler_barrier();
}

I would expect to see this:

if (_Order == memory_order_seq_cst) {
    volatile long _Guard;
    (void) _InterlockedIncrement(&_Guard);
} else {
    _Compiler_barrier();
}

If compiler barriers are actually needed for interlocked functions, I may have a lot of code to fix.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with stl/inc/atomic at the linked atomic_thread_fence implementation, then verify the compiler-barrier semantics of _InterlockedIncrement on the relevant MSVC targets. The issue is done when it is established whether the proposed barrier arrangement is correct and the implementation or related uses are updated if needed.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.