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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T02:42:41+00:00 2026-05-30T02:42:41+00:00

I am new to multi-threading programming, and confused about how Mutex works. In the

  • 0

I am new to multi-threading programming, and confused about how Mutex works. In the Boost::Thread manual, it states:

Mutexes guarantee that only one thread can lock a given mutex. If a code section is surrounded by a mutex locking and unlocking, it’s guaranteed that only a thread at a time executes that section of code. When that thread unlocks the mutex, other threads can enter to that code region:

My understanding is that Mutex is used to protect a section of code from being executed by multiple threads at the same time, NOT protect the memory address of a variable. It’s hard for me to grasp the concept, what happen if I have 2 different functions trying to write to the same memory address.

Is there something like this in Boost library:

  1. lock a memory address of a variable, e.g., double x, lock (x); So
    that other threads with a different function can not write to x.
  2. do something with x, e.g., x = x + rand();
  3. unlock (x)

Thanks.

  • 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-05-30T02:42:42+00:00Added an answer on May 30, 2026 at 2:42 am

    The mutex itself only ensures that only one thread of execution can lock the mutex at any given time. It’s up to you to ensure that modification of the associated variable happens only while the mutex is locked.

    C++ does give you a way to do that a little more easily than in something like C. In C, it’s pretty much up to you to write the code correctly, ensuring that anywhere you modify the variable, you first lock the mutex (and, of course, unlock it when you’re done).

    In C++, it’s pretty easy to encapsulate it all into a class with some operator overloading:

    class protected_int {
        int value; // this is the value we're going to share between threads
        mutex m;
    public:
        operator int() { return value; } // we'll assume no lock needed to read
        protected_int &operator=(int new_value) {
            lock(m);
            value = new_value;
            unlock(m);
            return *this;
        }
    };
    

    Obviously I’m simplifying that a lot (to the point that it’s probably useless as it stands), but hopefully you get the idea, which is that most of the code just treats the protected_int object as if it were a normal variable.

    When you do that, however, the mutex is automatically locked every time you assign a value to it, and unlocked immediately thereafter. Of course, that’s pretty much the simplest possible case — in many cases, you need to do something like lock the mutex, modify two (or more) variables in unison, then unlock. Regardless of the complexity, however, the idea remains that you centralize all the code that does the modification in one place, so you don’t have to worry about locking the mutex in the rest of the code. Where you do have two or more variables together like that, you generally will have to lock the mutex to read, not just to write — otherwise you can easily get an incorrect value where one of the variables has been modified but the other hasn’t.

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

Sidebar

Related Questions

Though standard doesn't guarantee the thread-safety for new , most of the multi-threading operating
I've written a simple multi-threaded game server in python that creates a new thread
Possible Duplicate: C# Captured Variable In Loop I am pretty new to multi-threading programming.
I am relatively new to multi-threading and want to execute a background task using
In my multi-threading server app, a thread per client is generated in this way:
I have a program where I use boost::threads for multi threading. Unfortunately drd (
Here I am again with questions about multi-threading and an exercise of my Concurrent
I'm very new to multi-threading and for some reason this class is giving me
I am relatively new to C#/.Net. I'm developing a desktop application that requires multi
I wanted to learn a little about multi threading in .NET and grabbed this

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.