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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T17:47:51+00:00 2026-05-12T17:47:51+00:00

I have a class that represents the state of the world. The class has

  • 0

I have a class that represents the “state of the world”. The class has many collections of many other objects which in turn have references to more objects and collections of objects, sometimes even references to their ancestors in the “world hierarchy”. To simplify what’s being said, here is an example (transformed into XML, details omitted):

<World>
<Country>
    <City>
        <Block>
        ...
        </Block>
    </City>
    <City>
        <Block>
        ...
        </Block>
    </City>
</Country>
...
<Country>
    <City>
        <Block> ... </Block>
        ...
        <Block> ... </Block>
    </City>
    <City>
        <Block> ... </Block>
    </City>
</Country>
</World>

There are two threads, the UI thread and the background thread (which is actually a server).

The server receives the messages which modify the “state of the world” (adding cities, blocks etc.).

The UI thread draws the world’s state to screen using PictureBox object every once in a while. The presentation layer has only reference to the IWorld object (which World implements), and it has no way to access the elements within.

The UI thread should lock the complete state of the world so the world can’t be changed (by the server) during the drawing (that would make an inconsistent world picture). Since it has only reference to the IWorld object, this is the only thing to be locked.

My question is whether this lock would suffice (that is, lock all fields and properties this object has, recursively), or each object should be locked separately. What is the right approach to this problem?

Note: UI has no way to contact server (meaning it can’t tell server to stop changing the world, and then tell it to resume after rendering).

EDIT:
If the World and all classes in hierachy implement ILock interface which provides Lock() method which would call Lock() on all lower levels (recursively), this would probably be prone to deadlock (cyclic references) or simply be too expensive.

I guess design change is in order.

  • 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-12T17:47:52+00:00Added an answer on May 12, 2026 at 5:47 pm

    You don’t need to lock recursively – if both threads just lock on the same IWorld reference, that will be enough to assure mutual exclusivity.

    Locking on the individual elements would give you finer grained locking of course, but then you could end up with an inconsistent picture – I would imagine that you’d want to see a whole set of changes from the server at a time.

    However, a better model would usually be to make the server publish “snapshots” and then create a new copy of the world and modify the copy, turning it into a snapshot when appropriate. That way you don’t need this mutual exclusion which could block your UI when it wants to draw the world while the server is changing it.

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

Sidebar

Related Questions

In my game engine I have a State class that represents the game world
Say I have a class which represents a person, a variable within that class
I have class that represents users. Users are divided into two groups with different
Here is my custom class that I have that represents a triangle. I'm trying
I have a MySQL file that represents my database in a specific state that
I have got a designing dilema. I have a class named UserAccount that represent
I have a ListItem class that is used to represent menu items in my
I have class that extend FragmentActivity in it I add fragment to layout as
I have class that looks like this: class A { public: class variables_map vm
I have class grades that I need to check if a specific grade is

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.