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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T15:34:42+00:00 2026-06-18T15:34:42+00:00

I would like to ask about the simplest Mutex approach ever for multi-threading. Is

  • 0

I would like to ask about the simplest Mutex approach ever for multi-threading. Is the following code thread-safe (quick-n-dirty)?

class myclass
{
    bool locked;
    vector<double> vals;

    myclass();
    void add(double val);
};

void myclass::add(double val)
{
    if(!locked)
    {
        this->locked = 1;
        this->vals.push_back(val);
        this->locked = 0;
    }
    else
    {
        this->add(val);
    }
}

int main()
{
    myclass cls;
    //start parallelism
    cls.add(static_cast<double>(rand()));
}

Does this work? Is it thread-safe? I’m just trying to understand how the simplest mutex can be written.

If you have any advice about my example, would be nice.

Thank you.

Thanks for saying that it doesn’t work. Can you please suggest a fix which is compiler independent?

  • 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-18T15:34:43+00:00Added an answer on June 18, 2026 at 3:34 pm

    Is it thread-safe?

    Certainly not. If a thread is preempted between checking and setting the lock, then a second thread could acquire that lock; if control then returns to the first thread, then both will acquire it. (And of course, on a modern processor, two or more cores could be executing the same instructions simultaneously for even more hilarity.)

    At the very least, you need an atomic test-and-set operation to implement a lock like this. The C++11 library provides such a thing:

    std::atomic_flag locked;
    
    if (!locked.test_and_set()) {
        vals.push_back(val);
        locked.clear();
    } else {
        // I don't know exactly what to do here; 
        // but recursively calling add() is a very bad idea.
    }
    

    or better yet:

    std::mutex mutex;
    
    std::lock_guard<std::mutex> lock(mutex);
    vals.push_back(val);
    

    If you have an older implementation, then you’ll have to rely on whatever extensions/libraries are available to you, as there was nothing helpful in the language or standard library back then.

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

Sidebar

Related Questions

I would like to ask you about some advices about this code. It works,
I would like to ask something about R's environments: In the next simple code
I would like to ask a couple of quick questions about the proper destruction
I would like to ask about best practices or the simplest way possible of
I would like to ask about the available (free or not) Static and Dynamic
I would like to ask you about regular expressions preg_match have outlined below. I
i would like to ask a question about @UsesJAXBContext annotation in jax-ws. I try
I have been reading about unsafePerformIO lately, and I would like to ask you
I would like to ask about Memory Overhead in java, I have a large
I would like to ask about the design of table based on it's editability

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.