add threads to a queue.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By vonflyhighace2
:warning: Old Version Published before Godot 3 was released.

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.

:bust_in_silhouette: Reply From: Warlaan

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).

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.

vonflyhighace2 | 2016-12-28 22:40

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.

Warlaan | 2016-12-29 11:13