Jump to content

Recommended Posts

Posted

Howdy folks

 

Are there any guidelines I should follow to ensure that my mod code is not going to cause multithreading problems?

 

Most of the time it is obvious because I know whether my code is in a render thread, client tick thread, server thread etc, and the method I'm overriding or implementing gives me the (thread-safe) object I need.

 

But I think there are some other places where there might be risk of concurrent threads colliding if I call vanilla objects; especially static registration methods of vanilla classes during mod initialisation.

 

Are ModEventBus events called concurrently if multiple mods are present?  If so, any calls I make to vanilla code from a ModEventBus event is likely to be risky I think.  I hear rumours that DeferredWordQueue::enqueueWork is the method to ensure that registrations using vanilla classes are executed single-threaded, is this the intended method?

 

What about the server thread, client thread?  Eg are there ever concurrent server threads which may cause my mod to access non-thread-safe objects or methods?  I know there are multiple render threads, which is perfectly safe provided I stick to the objects provided in the calling method or event and don't try to access server or client objects via 'sneaky' methods.

 

What assumptions is it safe to make?

 

The worst bugs I've ever had to deal with have all been caused by multithread collisions or race conditions so I'm very keen to understand where the high risk areas are...

 

Cheers

 TGG

 

Posted
7 minutes ago, diesieben07 said:

although the server uses separate threads for world generation (if I remember correctly)

This is what I believe happens as well. Each chunk may (or may not!) be on its own thread, so you can't reach out too far. I think/hope/assume that the +X/+Z chunks are accessible, due to how vanilla has always handled generation across chunk boundaries before, but I haven't had the time to actually find out. Its possible that there's some precompute (eg. structures) with the structure part bounding boxes and vanilla goes "ok, cool, you want to generate in this volume, that'd be this list of chunks...pooling chunks together...spawning thread...here you go, do what you need."

  • Thanks 1

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.