Shop OBEX P1 Docs P2 Docs Learn Events
Next available cog — Parallax Forums

Next available cog

The reference for the COGNEW command reads: 'Start the next available cog to run Spin code or Propeller Assembly code.'

What is the next available cog? If an object had launched code into what turns out to be Cog3, is the next available then Cog 4? If the object uses the Stop/Start public interface methods, if the object stops Cog 3 before the subsequent Cognew command, is the next available cog then Cog 3 again? If Cog 7 is in use, does the command start checking for a free cog starting at Cog 0 and counting up?

Dave

Comments

  • I don't think there's any way to predict this exactly. But more importantly, there's absolutely no good reason to. One's programs should never depend upon the availability of a particular cog.

    -Phil
  • The manual says that it starts the lowest numbered inactive cog. This can be relied upon.
  • Heater.Heater. Posts: 21,230
    Yes that can be relied on.

    In general it does not help. A program can be starting and stopping COGs at unpredictable times, depending on timing and object load/start order etc. Potentially the starting and stopping of COGs can happen in response to external events in which case it is effectively random. That "next COG available" is not knowable.

    A cog that tries to find out which is the next free COG can always be wrong. Having determined, somehow that COG N is free, some other running COG may start to use COG N before our first COG has a chance to do anything with what it discovered.

    Bottom line is that unless you have total control over when everything happens in your code the problem is, in general, impossible.

    And as Phil says, you don't need to know. A program should not care which COG is used to do what any more than it cares which memory locations are used to store its code and variables.

  • Thanks Phil and Mark. I am trying to discover why some poorly written code of mine fails the way it does. I have fixed the code in question, but I am still very curious why it failed and how. Nothing more than a chance for me to learn more, not for any actual use. I appreciate your taking the time to respond.

    Dave
Sign In or Register to comment.