cogstart question
GeorgeCollins
Posts: 132
I have been experimenting with programming a prop to do servos and a ping sensor simultaneously. I am somewhat used to threads in C or Java, and so as a test I tried the following code:
PUB TimeKiller | i,j
j:=0
repeat until (j > 1_000)
i:=i+1 'Do this forever
if (i>1_000)
i:=0
j:=j+1
This function is just meant to be a thing that takes a long time to test processing in parallel. And then in another function PUB Main ...
cog:=cognew(TimeKiller, @Stack)
TV.dec(cog)
cog:=cognew(TimeKiller, @Stack)
TV.dec(cog)
cog:=cognew(TimeKiller, @Stack)
TV.dec(cog)
cog:=cognew(TimeKiller, @Stack)
TV.dec(cog)
I would expect the numbers on the screen to be '3456' Indicating that the 3rd, 4th .. cog is started.
Instead I get '3344'
Why does cognew return the same value twice? Any thoughts suggestions?
Thank you for your help.
PS: The prop is sooo cool. It's been a learning curve to get the hardware working with servos and sensors. Now I am really excited to reap the power of multiprocessing on my robots.
PUB TimeKiller | i,j
j:=0
repeat until (j > 1_000)
i:=i+1 'Do this forever
if (i>1_000)
i:=0
j:=j+1
This function is just meant to be a thing that takes a long time to test processing in parallel. And then in another function PUB Main ...
cog:=cognew(TimeKiller, @Stack)
TV.dec(cog)
cog:=cognew(TimeKiller, @Stack)
TV.dec(cog)
cog:=cognew(TimeKiller, @Stack)
TV.dec(cog)
cog:=cognew(TimeKiller, @Stack)
TV.dec(cog)
I would expect the numbers on the screen to be '3456' Indicating that the 3rd, 4th .. cog is started.
Instead I get '3344'
Why does cognew return the same value twice? Any thoughts suggestions?
Thank you for your help.
PS: The prop is sooo cool. It's been a learning curve to get the hardware working with servos and sensors. Now I am really excited to reap the power of multiprocessing on my robots.
Comments
One thing that will surely give you difficulty is using the same stack for more than one cog. Each of your cognews needs to be given the address of a different stack.
-Phil
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
'Still some PropSTICK Kit bare PCBs left!
The Spin Interpreter uses the stack of the object executing CogNew() and also pre-loads part of the stack to be used by the launched method so perhaps there's some peculiar interaction in these circumstances. Perhaps it is execution of those launched methods where what's on the stack corrupted by another Cog is causing peculiar behaviour and corruption.
Use different stacks for each CogNew() and it should be okay