Reputation:
Trying to figure out if the coroutine is the right way to go.
I have 3 different classes that execute operations; I would like for these 3 classes to do their work in any order, and once all 3 are done, I can move on to the next cycle and call again methods in these 3 classes to start all over.
As now, I have a method in each class, that do the work; and in the main class, I created a coroutine, which is the main loop of my procedure. The coroutine has as condition that all 3 methods in the 3 classes has to be done, in a while loop that yield return void. This is a simplified version of the coroutine:
IEnumerator void loopFunction()
{
while (!classA.method() && !classB.method() && !classC.method())
{
yeld return void;
}
// do some updates
// start all over again
}
Would this be correctly implemented or should I use a different approach? The objective is to have a loop that execute every X seconds, and that a new cycle start only when all the methods in it are completed.
EDIT:
While browsing I found that is possible to actually use multithreading, the old "System.Threading" way thou, since Unity support only Net 3.5.
Threading seems to be another viable option; although I heard that Unity itself is not thread safe, so I am not sure if you can run the functions in the different classes, inside the coroutine, as separate threads.
Upvotes: 1
Views: 442
Reputation:
It depends how these methods will interact with the rest of your game. Coroutines provides a form of concurrency while threads provide a form of parallelism.
Only use threads if you actually need to parallelize operations, for the rest coroutines are perfectly fine.
Coroutines simply provides a mechanism to compose independently executing blocks of code where this execution is interleaved, not parallelized. There is basically no difference between a coroutine and any other general method/function except for its ability to (easily) suspend execution for a specific period.
Simply put, coroutines are about structure, threads are about execution. If you simply want to spread a set of operations across multiple frames, then coroutines offer a nice way to do this. Coroutines are executed on the main thread therefore other parts of the game will be suspended for as long as your three methods are running - your code is in control of the "suspend" action.
If your app is running on a hardware platform that can parallelize operations (multi-core or multi-processor) and you want to make use of that potential, then look at threads. Or if you make use of a 3rd party library and you have no control over how long a specific operation takes (and it takes too long), then you could consider threads too. Obviously that comes with a bit of added complexity, remember Unity is not thread safe so if your operations are in any way interacting with the Unity API you need to take care that your methods are synchronized appropriately via some sort of lock. Don’t access Unity objects directly from another thread.
Upvotes: 0