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 9156479
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:47:08+00:00 2026-06-17T12:47:08+00:00

posix standard says that things like mutex will enforce a memory sync. However, the

  • 0

posix standard says that things like mutex will enforce a memory sync.
However, the compiler may reorder the memory access.
Say we have

lock(mutex);
setdata(0);
ready = 1;
unlock(mutex);

It might be changed to code below by compiler reordering, right?

ready = 1;
lock(mutex);
setdata(0);
unlock(mutex);

So how can mutex sync the memory access? To be more precise, how do compilers know that reordering should not happen across lock/unlock?

actually here for single thread aspect, ready assignment reorder is totally safe since ready is not used in function call lock(mutex).

EDITED:
So if function call is something that compiler will not get across,
can we regard it as a compiler memory barrier like

asm volatile("" ::: "memory")
  • 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-17T12:47:09+00:00Added an answer on June 17, 2026 at 12:47 pm

    General answer is that your compiler should support POSIX if you want to use it for POSIX targets, and that support means it should know to avoid reordering across lock and unlock.

    That said, this kind of knowledge is commonly achieved in a trivial way: compiler would not reorder access to (non-provably-local) data across a call to an external function which may use or modify them. It should have known something special about lock and unlock to be able to reorder.

    And no, it’s not that simple as “a call to global function is always a compiler barrier” — we should add “unless the compiler knows something specific about that function”. It does really happen: e.g. pthread_self on Linux (NPTL) is declared with __const__ attribute, allowing gcc to reorder across pthread_self() calls, even eliminating unnecessary calls altogether.

    We can easily imagine a compiler supporting function attributes for acquire/release semantics, making lock and unlock less than a full compiler barrier.

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

Sidebar

Related Questions

Does the POSIX standard allow a named shared memory block to contain a mutex
Is there a POSIX function that works like the which command? That is, I
I am writing a POSIX shell script that may or may not receive input
How do you conserve memory when receiving messages from POSIX queues? It seems that
How similar/different are gnu make, microsoft nmake and posix standard make? Obviously there's things
Unfortunately the cygwin GCC 4.5.3 pthread library implementation doesn't support the POSIX standard function
Is there a POSIX function that searches PATH for an executable according to the
Is there any possibility to achieve different redirections for standard output like printf(3) for
Does the POSIX standard or another C standard provide a way to recover a
Many standard C and POSIX functions return -1 for error, and 0 on success,

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.