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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T02:43:19+00:00 2026-06-11T02:43:19+00:00

I’ve made a hand-made mutex for my project, but I doubt if it is

  • 0

I’ve made a hand-made mutex for my project, but I doubt if it is thread safe…

    bool blocked;

    while ( blocked )
    {

    }

    blocked = true;
    ...
    blocked = false;

Lets say, thread A passes the while loop and doesn’t block the flag in time (doesn’t have time to set the flag to false) and thread B passes the while loop too!

  1. Is it possible? Why?

  2. As I get it mutex has the very same principle of work. Why can not this occur in mutex? I’ve read about atomic operations which can not be interrupted… So the check-if-mutex-available and mutex-block can not be interrupted, right?

  • 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-11T02:43:20+00:00Added an answer on June 11, 2026 at 2:43 am

    Your code is completely defunct!

    The reason is that access to the variable blocked is not atomic. Two threads can read it si­mul­ta­ne­ous­ly and decide that the mutex is unlocked, if the two reads happen before the first thread writes out the true update and the update propagates to all CPUs.

    You need atomic variables and atomic exchange to solve this. The atomic_flag type is exactly what you want:

    #include <atomic>
    
    std::atomic_flag blocked;
    
    while (blocked.test_and_set()) { }  // spin while "true"
    
    // critical work goes here
    
    blocked.clear();                    // unlock
    

    (Alternatively, you could use a std::atomic<bool> and exchange(true), but the atomic_flag is made especially for this purpose.)

    Atomic variables do not only prevent the compiler from reordering code that appears to be unrelated if this were a single-threaded context, but they also make the compiler generate the necessary code to prevent the CPU itself from reordering instructions in a way that would allow inconsistent execution flow.

    In fact, if you want to be slightly more efficient, you can demand a less expensive memory ordering on the set and clear operations, like so:

    while (blocked.test_and_set(std::memory_order_acquire)) { }  // lock
    
    // ...
    
    blocked.clear(std::memory_order_release);                    // unlock
    

    The reason is that you only care about the correct ordering in one direction: a delayed update in the other direction is not very expensive, but requiring sequential consistency (as is the default) may well be expensive.


    Important: The above code is a so-called spin lock, because while the state is locked, we do a busy spin (the while loop). This is very bad in almost all situations. A kernel-provided mutex system call is a whole different kettle of fish, since it allows the thread to signal the kernel that it can go to sleep and let the kernel deschedule the entire thread. That is almost always the better behaviour.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
Seemingly simple, but I cannot find anything relevant on the web. What is the
I want to construct a data frame in an Rcpp function, but when I
I need to clean up various Word 'smart' characters in user input, including but
public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim

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.