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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T12:35:17+00:00 2026-05-16T12:35:17+00:00

I have a bunch of different methods that are not supposed to run concurrently,

  • 0

I have a bunch of different methods that are not supposed to run concurrently, so I use a single lock to synchronize them. Looks something like this:

selected_method = choose_method()
with lock:
    selected_method()

In some of these methods, I sometimes call a helper function that does some slow network IO. (Let’s call that one network_method()). I would like to release the lock while this function is running, to allow other threads to continue their processing.

One way to achieve this would be by calling lock.release() and lock.acquire() before and after calling the network method. However, I would prefer to keep the methods oblivious to the lock, since there are many of them and they change all the time.

I would much prefer to rewrite network_method() so that it checks to see whether the lock is held, and if so release it before starting and acquire it again at the end.

Note that network_method() sometimes gets called from other places, so it shouldn’t release the lock if it’s not on the thread that holds it.

I tried using the locked() method on the Lock object, but that method only tells me whether the lock is held, not if it is held by the current thread.

By the way, lock is a global object and I’m fine with that.

  • 1 1 Answer
  • 1 View
  • 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-16T12:35:17+00:00Added an answer on May 16, 2026 at 12:35 pm

    I would much prefer to rewrite network_method() so that it checks to see whether the lock is held, and if so release it before starting and acquire it again at the end.

    Note that network_method() sometimes gets called from other places, so it shouldn’t release the lock if it’s not on the thread that holds it.

    This just sounds like entirely the wrong thing to do 🙁

    For a start, it’s bad to have a function that sometimes has some other magical side-effect depending on where you call it from. That’s the sort of thing that is a nightmare to debug.

    Secondly, a lock should have clear acquire and release semantics. If I look at code that says "lock(); do_something(); unlock();" then I expect it to be locked for the duration of do_something(). In fact, it is also telling me that do_something() requires a lock. If I find out that someone has written a particular do_something() which actually unlocks the lock that I just saw to be locked, I will either (a) fire them or (b) hunt them down with weapons, depending on whether I am in a position of seniority relative to them or not.

    By the way, lock is a global object and I’m fine with that.

    Incidentally, this is also why globals are bad. If I modify a value, call a function, and then modify a value again, I don’t want that function in the middle being able to reach back out and modify this value in an unpredictable way.

    My suggestion to you is this: your lock is in the wrong place, or doing the wrong thing, or both. You say these methods aren’t supposed to run concurrently, but you actually want some of them to run concurrently. The fact that one of them is "slow" can’t possibly make it acceptable to remove the lock – either you need the mutual exclusion during this type of operation for it to be correct, or you do not. If the slower operation is indeed inherently safe when the others are not, then maybe it doesn’t need the lock – but that implies the lock should go inside each of the faster operations, not outside them. But all of this is dependent on what exactly the lock is for.

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

Sidebar

Related Questions

I have a bunch of third-party Java classes that use different property names for
I have a bunch of methods that make use of jquery to basically generate
I have a bunch of different objects that are commonly edited in the same
Let's say that I have a bunch of class instances that serve different purposes,
I have two branches that are different enough that rebasing seems to not work
I'm using tornado, and have a bunch of handlers that map to different urls.
I have looked at this a bunch of different ways and with what little
In my DB I have a bunch of listings with different city names. Im
I have a bunch of code of the form: someVector.push_back(Foo(some name, 1.0, 3.1415926); someVector.push_back(Foo(different
I'm working with ASP.NET MVC 2. I have a bunch of partial views that

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.