How long does it take for a cog to start - and when does SPIN cognew return?
ags
Posts: 386
I just finished debugging a problem that was solved (or the problem masked, as I can't yet explain the behavior I've observed) by adding a pause after starting another cog. In my top object, I call another object's Init method to start a new cog (the "child" cog) which launches PASM code, then call methods that run in the original cog (the "parent" cog - the one from which Init was called to start the "child" cog) and which interact with the "child" cog. If this description is not clear I can post some code.
My hypothesis is that (SPIN) cognew returns before the cog which it starts has loaded and launched the PASM code and stabilized. So if the next method I call requires the other ("child") cog to be running for correct function, it fails. If cognew waits for the new cog to be up and running before returning, this theory doesn't make sense.
I note that just a single 1 second pause immediately after cognew returns solves the problem; no other pauses are necessary.
So when does cognew return, and how long does it take for a newly started cog (executing PASM code) to be ready to process requests?
My hypothesis is that (SPIN) cognew returns before the cog which it starts has loaded and launched the PASM code and stabilized. So if the next method I call requires the other ("child") cog to be running for correct function, it fails. If cognew waits for the new cog to be up and running before returning, this theory doesn't make sense.
I note that just a single 1 second pause immediately after cognew returns solves the problem; no other pauses are necessary.
So when does cognew return, and how long does it take for a newly started cog (executing PASM code) to be ready to process requests?
Comments
It is considered a best practice to always add a delay in the initializing method after starting a cog to avoid this type of problem? This type of problem is hard to diagnose, particularly if the same object worked in other applications (because the caller didn't hit the new cog with an immediate method call).
Thanks for another helpful answer. I'm surprised that I haven't hit this before, or seen it discussed here.
Personally I don't like the style. I would rather the parent cog waits for some flag to be set (or cleared) by the child when it is up and running and ready to do business. There are many approaches to this.
One line of code to change. Brilliant. Thanks.