Stop-ping for Cognews?
coryco2
Posts: 107
I was reading in the Propeller Spin Language Reference in the Cognew section how one could use a "Stop" method to prevent multiple starts when starting code in a new cog...
...and thought how this would be smart to implement in my own code. However, when I tried it on the following, Method4 never runs. Why not? I even tried adding "return" in each of the Stop methods, but to no avail.
PUB Start Stop CogID := cognew (Method, @StackSpace) PUB Stop if CogID > -1 cogstop (Cog ID)
...and thought how this would be smart to implement in my own code. However, when I tried it on the following, Method4 never runs. Why not? I even tried adding "return" in each of the Stop methods, but to no avail.
PUB Start StopA CogA := cognew(Method1, @CogAStack) StopB CogB := cognew(Method2, @CogBStack) StopC CogC := cognew(Method3, @CogCStack) Method4 PUB StopA if CogA > -1 cogstop (CogA) PUB StopB if CogB > -1 cogstop (CogB) PUB StopC if CogC > -1 cogstop (CogC)
Comments
However, there is no need to have 3 different Stop-methods! You could replace it with:
As to the rest of the code, it works fine without these Stop methods, so it's something about them that is causing the issue. Some of the additional Cog methods do themselves start other cogs (for FullDuplexSerial, etc.); so is it possible that I am just running out of cogs for some reason, or stopping the wrong cogs? I don't how that could happen, though...
Since my code works without the Stop calls, it seems logical to me that the only way it will not start Method4 is if it is getting stuck in somewhere in one of those Stop calls. But why?
If MagIO2's is willing to help, I'd suggest accepting.
IMO, he knows a lot about this stuff.
So, if you don't have secret code to hide from us, you should archive the project and post the whole package here.
If it IS secret, you have to find the bug by yourself, as the part you posted should be fine!
If real strange things go on, you may have problems with the stack-size.
The point is, that variables are initialized with 0. So, calling stop will actually kill the main-COG.
You should initialize the CogA..C variables before first usage or better switch to the following code:
The basic trick here is, that the COGID is incremented by one. So, if cognew returns -1 (no more COG available) you get 0 which is a) the same value that all VARs are initialized to and b) is equal to FALSE in an if statement.
a) means that you don't need extra initialization code (defaulting the Cog-ID variables to -1)
b) means that your if statement does not need a compare
This should be corrected and explained in the manual!
Thank you for pointing this out. I've added it to our Manual change list. I'm glad you guys have our back!
Best regards,
Daniel