Shop OBEX P1 Docs P2 Docs Learn Events
Question on cog termination: — Parallax Forums

Question on cog termination:

andrewsiandrewsi Posts: 59
edited 2010-10-27 17:19 in Propeller 1
Let's say you have a task that you want to run in a separate cog because it's potentially lengthy (e.g. a long lock on a semaphore) and you don't want to block your main cog's execution, but you want it to terminate when it's complete and release its cog back to the pool. (Perhaps this is all faulty program design on my part, but bear with me for a moment. :smilewinkgrin:)

Will the cog be returned to the pool automatically when its method's execution ends, or will it remain "assigned" indefinitely unless something explicitly calls Cogstop on its Cog ID?

Thanks-
Andy

Comments

  • andrewsiandrewsi Posts: 59
    edited 2010-10-27 16:09
    Think I've answered my own question by the Q&A page (excellent!)

    Just use COGSTOP(CogID) at the end of the method, to shut itself down.
  • Mike GreenMike Green Posts: 23,101
    edited 2010-10-27 16:26
    It's even simpler than that. When you do a COGNEW or COGINIT, the return address for the call to the method (subroutine) to be started in the new cog is forced to be to a COGSTOP. When the main method for that cog exits, the cog gets stopped automatically.
  • andrewsiandrewsi Posts: 59
    edited 2010-10-27 16:28
    So that basically answers my question - the cog gets returned to the pool since there's an implicit COGSTOP if it runs out of code?
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2010-10-27 17:19
    andrewsi wrote:
    ... the cog gets returned to the pool since there's an implicit COGSTOP if it runs out of code?
    Yes, that's true of Spin cogs, but not PASM cogs. Those require a cogstop; otherwise, they will just keep looping, rolling over from $1ff to $000. Also, if you have a Spin cog that's finished with its computations but needs to keep output pins or counters in a certain state, you can end it with a naked repeat to keep it alive.

    -Phil
Sign In or Register to comment.