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

  • Home
  • SEARCH
  • 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 122541
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T04:08:42+00:00 2026-05-11T04:08:42+00:00

If the locks make sure only one thread accesses the locked data at a

  • 0

If the locks make sure only one thread accesses the locked data at a time, then what controls access to the locking functions?

I thought that boost::mutex::scoped_lock should be at the beginning of each of my functions so the local variables don’t get modified unexpectedly by another thread, is that correct? What if two threads are trying to acquire the lock at very close times? Won’t the lock’s local variables used internally be corrupted by the other thread?

My question is not boost-specific but I’ll probably be using that unless you recommend another.

  • 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. 2026-05-11T04:08:42+00:00Added an answer on May 11, 2026 at 4:08 am

    You’re right, when implementing locks you need some way of guaranteeing that two processes don’t get the lock at the same time. To do this, you need to use an atomic instruction – one that’s guaranteed to complete without interruption. One such instruction is test-and-set, an operation that will get the state of a boolean variable, set it to true, and return the previously retrieved state.

    What this does is this allows you to write code that continually tests to see if it can get the lock. Assume x is a shared variable between threads:

    while(testandset(x)); // ... // critical section // this code can only be executed by once thread at a time // ... x = 0; // set x to 0, allow another process into critical section 

    Since the other threads continually test the lock until they’re let into the critical section, this is a very inefficient way of guaranteeing mutual exclusion. However, using this simple concept, you can build more complicated control structures like semaphores that are much more efficient (because the processes aren’t looping, they’re sleeping)

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

Sidebar

Related Questions

If I am using ReaderWriterLockSlim to acquire read/write locks, do I need to make
I'm trying to learn redcode, because it looks fun to make a bot. Introduction
How do I avoid read locks in my database? Answers for multiple databases welcome!
I was looking at the source code of java.uti.concurrent.locks.AbstractQueuedSynchronizer and the acquire() method looks
When my script sleeps for 50sec my IDE locks up which is very annoying.
I've got a bunch of properties which I am going to use read/write locks
I need to do transactions (begin, commit or rollback), locks (select for update). How
I find System.Monitor very confusing, although I understand threading, locks, deadlocks, race conditions, dining
This question is NOT about race-conditions, atomicity, or why you should use locks in
I asked a question about building custom Thread Safe Generic List now I am

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.