Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9225485
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:35:17+00:00 2026-06-18T04:35:17+00:00

cppreference.com documents this function as fence between a thread and a signal handler executed

  • 0

cppreference.com documents this function as “fence between a thread and a signal handler executed in the same thread”. But I found no example on the Internet.

I wonder whether or not the following psuedo-code correctly illustrates the function of std::atomic_signal_fence():

int n = 0;
SignalObject s;

void thread_1()
{
    s.wait();
    std::atomic_signal_fence(std::memory_order_acquire);
    assert(1 == n); // never fires ???
}

void thread_2()
{
    n = 1;
    s.signal();
}

int main()
{
    std::thread t1(thread_1);
    std::thread t2(thread_2);

    t1.join(); t2.join();
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T04:35:18+00:00Added an answer on June 18, 2026 at 4:35 am

    No, your code does not demonstrate correct usage of atomic_signal_fence. As you quote cppreference.com, atomic_signal_fence only perform synchronization between a signal handler and other code running on the same thread. That means that it does not perform synchronization between two different threads. Your example code shows two different threads.

    The C++ spec contains the following notes about this function:

    Note: compiler optimizations and reorderings of loads and stores are inhibited in the same way as with atomic_thread_fence, but the hardware fence instructions that atomic_thread_fence would have inserted are not emitted.

    Note: atomic_signal_fence can be used to specify the order in which actions performed by the thread become visible to the signal handler.

    Here’s an example of correct, if not motivating, usage:

    static_assert(2 == ATOMIC_INT_LOCK_FREE, "this implementation does not guarantee that std::atomic<int> is always lock free.");
    
    std::atomic<int> a = 0;
    std::atomic<int> b = 0;
    
    extern "C" void handler(int) {
        if (1 == a.load(std::memory_order_relaxed)) {
            std::atomic_signal_fence(std::memory_order_acquire);
            assert(1 == b.load(std::memory_order_relaxed));
        }
    
        std::exit(0);
    }
    
    int main() {
        std::signal(SIGTERM, &handler);
    
        b.store(1, std::memory_order_relaxed);
        std::atomic_signal_fence(std::memory_order_release);
        a.store(1, std::memory_order_relaxed);
    }
    

    The assertion, if encountered, is guaranteed to hold true.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The C++ operator precedence table from http://en.cppreference.com/w/cpp/language/operator_precedence (I know it's not normative, but the
Consider the std::rel_ops example here: http://en.cppreference.com/w/cpp/utility/rel_ops/operator_cmp #include <iostream> #include <utility> struct Foo { int
I'm reading different interpretations of the way this function should work. cplusplus.com says that
In the example of this description of packaged_task from cppreference, a class named task
Here: http://en.cppreference.com/w/cpp/utility/functional/function operator bool is described: Checks whether the stored callable object is valid.
I've looked at cppreference.com and they seem to indicate no noexcept specification on std::function(std::function&&)
According to http://en.cppreference.com/w/cpp/string/byte/memcpy C++'s memcpy takes three parameters: destination, source and size/bytes. It also
I want to reference a COM DLL in a .NET project, but I also
Using http://www.cppreference.com/wiki/stl/deque/insert as a reference, I was inserting values into a deque at certain
As stated in http://en.cppreference.com/w/cpp/error/terminate there are many reasons to call terminate. I can imagine

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.