0 votes

Is it possible to add running threads to a queue. I have some methods that run in another thread but sometimes one is called right after another before the first finishes. right now I'm blocking any other thread from running by checking if the thread is already active. that unfortunately ignores the second method and it does not get run.

in Engine by (271 points)

1 Answer

0 votes

Would you mind showing your code? It sounds like you are creating a new thread for each call, otherwise it's simply impossible to start a new call before another one has finished (unless you are killing the thread and replacing it with another one).

by (1,120 points)

Its kind of difficult to showcase because the class is such a large one but the gist of it is that I have a class that acts like a server class. I also have many other class's that inherits from this server class. the server class as a variable that is of type thread. At any given time these inherited class can access this variable and issue work on the thread variable. My problem I think is that multiple class are accessing and issuing work at the same time so it might be like you said and they are over-writing each other. Any Ideas of how to prevent or work around this? I'm still trying to grasp the concept of threading.

I think it's a lack of understanding of object orientation that is causing the problem.

When you have a base class with a variable and several child classes, these child classes have individual instances of that variable. Let's say I have a base class "tree" and two child classes "oak" and "palm". The tree has a height and so both oak and palm also have heights. But those heights are individual instances of the value, meaning that if I change the height of an oak it doesn't affect the height of other trees.

By inheriting a class you are creating an "isA-relationship", meaning that e.g. an oak IS A tree, and since every tree has a height so has an oak. In your case you have several classes that ARE servers, so each one of them has a thread of their own.
If you want them to share the same thread you need to create one instance of the server class and then create "hasA-relationships", meaning that every class that is supposed to use that thread needs to have a member that points at the same server instance.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.