Shop OBEX P1 Docs P2 Docs Learn Events
Cogs — Parallax Forums

Cogs

codekingcodeking Posts: 39
edited 2007-01-08 01:42 in Propeller 1
Does a cog stop once the function it's set to run is finished so you can set another function to it with cognew? Or do you have to set another function with coginit?

Post Edited (codeking) : 1/7/2007 5:09:18 PM GMT

Comments

  • Jasper_MJasper_M Posts: 222
    edited 2007-01-07 17:30
    Yes. A cog stops automatically when the function is done and cognew can be used.
  • Mike GreenMike Green Posts: 23,101
    edited 2007-01-07 18:21
    That said, it's not how cogs are normally used. The time needed to start up a cog is substantial ... it takes 100's of microseconds mostly because the 2K bytes of cog ram has to be copied from hub ram. You wouldn't want to do this often. More typically, a cog (if it's not purely an I/O driver) is used as a kind of interpreter and accepts commands from some shared area of hub ram. It runs continuously waiting for something to do and never really quits unless stopped by some other part of the program.
  • codekingcodeking Posts: 39
    edited 2007-01-07 23:00
    So if I used coginit on an already created cog it would go much faster?
  • Graham StablerGraham Stabler Posts: 2,510
    edited 2007-01-08 00:50
    codeking, I think there may be some confusion over "its function"

    You could write some code that was just loaded into a cog, ran to the end and then stopped. Once it had completed "its function" it would stop and the cog would be free.

    But if you look at most programs an object is loaded into the cog that runs continously in a loop, the object provides functions that allow the user to access the data the cog produces and also send it data. These functions do not require the cog to be restarted every time they are used. The cog keeps on going until it is stopped.

    It is better to have this sort of program that runs continously because you do not have the cog load delay before each function call. If however it was a one off process used to set up some hardware then it might make sense.

    Why would you want to use coginit on an already initialized cog??? I suspect you need to revist the manual.

    Graham
  • Mike GreenMike Green Posts: 23,101
    edited 2007-01-08 01:42
    codeking,
    Using coginit on an already created cog would not help. The major delay is in reloading the cog's memory and both coginit and cognew do this as part of the startup process. It is not possible to initialize a cog without reloading its memory (completely!) This was designed in to prevent the Spin interpreter from being copied (among other things).
Sign In or Register to comment.