Shop OBEX P1 Docs P2 Docs Learn Events
cogstart question — Parallax Forums

cogstart question

GeorgeCollinsGeorgeCollins Posts: 132
edited 2008-09-29 17:30 in Propeller 1
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.

Comments

  • GeorgeCollinsGeorgeCollins Posts: 132
    edited 2008-09-29 03:30
    Oh, also, looking at my post the indenting looks wrong in the function "TimeKiller" but it is correct in the editor (something didn't paste right) and I know it works fine from calling it without cogstart.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2008-09-29 03:48
    To maintain indentation in your post, put your program between [noparse][[/noparse]code] and [noparse][[/noparse]/code] tags.

    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!
  • hippyhippy Posts: 1,981
    edited 2008-09-29 12:29
    It's interesting though that a different Cog Id isn't returned each time regardless that the same stack is used and the methods launched will all 'go to pot' in executing.

    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
  • GeorgeCollinsGeorgeCollins Posts: 132
    edited 2008-09-29 17:30
    Thank you! It seems obvious now, but who knows how long it might have taken to figure out on my own.
Sign In or Register to comment.