Does running CogNew create a copy of all my object's vars?
Dennis Ferron
Posts: 480
I'm trying to debug a Spin object where my start routine launches a worker subroutine second Spin cog. I'm trying to use some vars to communicate between the parent object and the subroutine started by the parent, but it seems like when I try to access those vars in the subroutine, all I get are copies of the variables. For instance:
So, I'm guessing cognew on a Spin routine creates a copy of the object? What if you had a lot of data used by the worker routine, that isn't needed by the Start'ing object; is there are way to avoid making 2 copies?
If it is another copy, I wonder if there is a hack to set the value of the object base address pointer within the DoWork routine so that it points again at the original object, so that referring to the variables refers to the original ones and not the copy? It would be something like cognew(DoWork(@@0), @Stack) and DoWork(base_address) would reset its own base address to the value passed in?
VAR long TestComm long Stack[noparse][[/noparse]16] PUB Start TestComm := 11 cognew(DoWork, @Stack) repeat TestComm := 22 PRI DoWork repeat ' Print out TestComm: value is 11, never prints 22
So, I'm guessing cognew on a Spin routine creates a copy of the object? What if you had a lot of data used by the worker routine, that isn't needed by the Start'ing object; is there are way to avoid making 2 copies?
If it is another copy, I wonder if there is a hack to set the value of the object base address pointer within the DoWork routine so that it points again at the original object, so that referring to the variables refers to the original ones and not the copy? It would be something like cognew(DoWork(@@0), @Stack) and DoWork(base_address) would reset its own base address to the value passed in?
Comments
There's only one copy of TestComm. Perhaps there's something in the timing between the two cogs. It's impossible to tell from the short fragments you've supplied.
Substituting 'TestComm' with an LED pin number on the Propeller DEMO board, I get the secondary number to pass through. There must be something in another part of your code that causing the problem.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Beau Schwabe
IC Layout Engineer
Parallax, Inc.
cognew( @DoWork, @stack)
That's why you see value 11 and never 22. The repeat loop is never reached and the print loop is executed by cog 0.
That only applies to an PASM program. SPIN just requires the spin method.
COGNEW (SpinMethod <(ParameterList)>, StackPointer)
COGNEW (AsmAddress, Parameter)
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Beau Schwabe
IC Layout Engineer
Parallax, Inc.
And as you see I currently prefer running PASM code in all other COGs.
So I'd be really interested what Dennis did wrong in the code he run. The posted code looks to be correct.