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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T03:05:37+00:00 2026-05-25T03:05:37+00:00

I have the following pseudo code: class A: mutex lockForB class B: def __init__(self,

  • 0

I have the following pseudo code:

class A:
    mutex lockForB

class B:
    def __init__(self, A): //the type of A is class A
        lock(A.lockForB)
        # ...
        unlock(A.lockForB)

    # other function have the same locking

I understand from oop point of view it is very bad idea to use such design, but if I create lock inside class B I will not be able to put lock on creator of the class B. Is there any better design for this? Thanks in advance.

  • 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-25T03:05:38+00:00Added an answer on May 25, 2026 at 3:05 am

    I have no idea what you’re trying to accomplish. It’s unlikely that a class level lock is what you want but your pseudocode is not so far from the actual code so I’ll just fill in the blanks. Honestly, without some idea what you’re attempting to synchronize access to it’s going to be a challenge to help you.

    class A:
        lockForB = threading.RLock()
    
    class B:
        def __init__(self): 
            with A.lockForB:
                # do init stuff
    
        def othermethod(self):
            with A.lockForB:
                # do other stuff
    

    So that code will work. lockForB is just a class level attribute on A so it is shared between all instances of A. However, in cases where I’ve seen folks use class level locks like this it is usually to prevent the class that owns the lock from being put into an inconsistent state where you have 2 seemingly unrelated classes sharing a lock.

    Without context to help understand what you’re attempting to synchronize access to it’s really hard to tell you why this couldn’t be written like this:

    class C:
        lock = threading.RLock()
        def __init__(self):
            with self.lock:
                # do stuff
        def othermethod(self):
            with self.lock:
                # do other stuff
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following piece of pseudo-C/Java/C# code: int a[]= { 30, 20 };
I have following situation. In a constructor of a pseudo class I attach a
i have following pseudo code : void siftup(int n) pre condition n>0 && heap(1,n-1)
I have a test database design like this: The following is the pseudo-code: //BhillHeader
I have a following pseudo-code. public boolean onContextItemSelected(MenuItem aItem) { switch(aItem.getItemId()) { case A:
I have the following problem illustrated by the pseudo code below (might not make
following piece of code: class a{ public function __get($key) { if($key == 'obj') {
I have the following pseudo-SQL schema: table flight id int primary key date timestamp
I have following string String str = replace :) :) with some other string;
I have following situation. A main table and many other tables linked together with

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.