I'm trying to get a good grasp of multithreading so what I did was create a threading action that returns the first thread that founds a given number, but I also want the other threads to stop once the first thread finds the number.
In so many ways it is hard to know where to start...
Galaxy (the scripting language used by StarCraft II, and what GUI compiles to) does not really have an idea of multithreading. Sure it can create new "threads", however these threads are really pseudo threads. They are not pre-emptive, are scheduled in the same physical thread and have a very well defined execution order. No matter how many threads you create, only 1 will run at the same time and that thread will run from the start until it either terminates, blocks (eg Wait action) or yields (action triggering another event? not sure this is possible in SC2 but was in WC3) to allow another thread to run. The thread which is next run is determined by the order the threads were created, blocked or yielded in. All trigger execution can be considered instant with respect to game relative time.
In StarCraft II what you should be using multithreading for is to run non-instantaneous actions in a way that does not block the thread from executing. For example you could create a chain explosion action which makes 3 explosions at a point, 1 every second achieved by using a wait action. By making that chain explosion action threaded you can use a trigger to instantly create 3 of them instead of it running through the first one over 3 seconds then starting on the next etc. Multithreading can also be used to bypass the instantaneous oplimit for a single thread however there are very few situations which require this and in almost all of them one can expect frame drops as a result as the oplimit of a single thread is already not trivial.
What am I doing wrong?
Trying to use Galaxy to learn/grasp multithreading. You really should be using a language like C++ (modern versions) or Java to learn multithreading. I personally would recommend Java as it is very easy to setup, requiring the freely available JDK (Java Development Kit) which can be downloaded from Oracle and an IDE like Eclipse. There should be a large variety of documentation on the internet with examples demonstrating multithreading techniques.
Multithreading is not an easy subject. Even experienced programmers still get headaches when dealing with it. This is why StarCraft II only uses 2 heavy threads, one for advancing the game state and the other for the actor subsystem.
If you really must do it in StarCraft II then you will need to add a wait into the loop of the thread action of at least 1 frame (1/16 game second) so that the different threads appear to run in parallel. Global variables (not local variables) can be used to communicate between threads by having the threads poll the variables each iteration. Be aware that sharing global variables this way is not possible in real multithreading such as C++ and Java due to the memory model used requiring the use of synchronization structures to assure value consistency between threads.
Thank you for the response. I already have a grasp around multi-threading in general, it's just that I wasn't sure about how multithreading works in this editor. My logic should have been correct but I didn't know these threads are actually "pseudo-threads". That's disappointing.
Is there a way to command an individual thread to "stop" and continue on the next thread and so on and so on indefinitely? Or rather what commands can I give to each individual thread?
I do not think there is a functionality to get a handle of a thread. As such there are no direct ways to command them. Best one can do is use a global messaging system and get the thread to poll for messages every time it wakes up. The message data can be stored in either global variables or in the global data table.
I'm trying to get a good grasp of multithreading so what I did was create a threading action that returns the first thread that founds a given number, but I also want the other threads to stop once the first thread finds the number.
Is my logic wrong? What am I doing wrong?
In so many ways it is hard to know where to start...
Galaxy (the scripting language used by StarCraft II, and what GUI compiles to) does not really have an idea of multithreading. Sure it can create new "threads", however these threads are really pseudo threads. They are not pre-emptive, are scheduled in the same physical thread and have a very well defined execution order. No matter how many threads you create, only 1 will run at the same time and that thread will run from the start until it either terminates, blocks (eg Wait action) or yields (action triggering another event? not sure this is possible in SC2 but was in WC3) to allow another thread to run. The thread which is next run is determined by the order the threads were created, blocked or yielded in. All trigger execution can be considered instant with respect to game relative time.
In StarCraft II what you should be using multithreading for is to run non-instantaneous actions in a way that does not block the thread from executing. For example you could create a chain explosion action which makes 3 explosions at a point, 1 every second achieved by using a wait action. By making that chain explosion action threaded you can use a trigger to instantly create 3 of them instead of it running through the first one over 3 seconds then starting on the next etc. Multithreading can also be used to bypass the instantaneous oplimit for a single thread however there are very few situations which require this and in almost all of them one can expect frame drops as a result as the oplimit of a single thread is already not trivial.
Trying to use Galaxy to learn/grasp multithreading. You really should be using a language like C++ (modern versions) or Java to learn multithreading. I personally would recommend Java as it is very easy to setup, requiring the freely available JDK (Java Development Kit) which can be downloaded from Oracle and an IDE like Eclipse. There should be a large variety of documentation on the internet with examples demonstrating multithreading techniques.
Multithreading is not an easy subject. Even experienced programmers still get headaches when dealing with it. This is why StarCraft II only uses 2 heavy threads, one for advancing the game state and the other for the actor subsystem.
If you really must do it in StarCraft II then you will need to add a wait into the loop of the thread action of at least 1 frame (1/16 game second) so that the different threads appear to run in parallel. Global variables (not local variables) can be used to communicate between threads by having the threads poll the variables each iteration. Be aware that sharing global variables this way is not possible in real multithreading such as C++ and Java due to the memory model used requiring the use of synchronization structures to assure value consistency between threads.
Thank you for the response.
I already have a grasp around multi-threading in general, it's just that I wasn't sure about how multithreading works in this editor. My logic should have been correct but I didn't know these threads are actually "pseudo-threads". That's disappointing.
Is there a way to command an individual thread to "stop" and continue on the next thread and so on and so on indefinitely? Or rather what commands can I give to each individual thread?
I do not think there is a functionality to get a handle of a thread. As such there are no direct ways to command them. Best one can do is use a global messaging system and get the thread to poll for messages every time it wakes up. The message data can be stored in either global variables or in the global data table.
Thank you!
I added a global variable to monitor the threads and a boolean global variable like you said and it works great, just what i wanted.