Shop OBEX P1 Docs P2 Docs Learn Events
Stack Memory — Parallax Forums

Stack Memory

JoeFLJoeFL Posts: 10
edited 2011-11-30 11:50 in Propeller 1
I'm writing my first Spin program and have a couple of questions.

1. When starting multiple Cogs, how can I be sure I'm allocating enough Stack space for each Cog?
2. Is sharing global variables between Cogs as easy are declaring Global variables in Cog_0
then just reading them in other Cogs, such as

Cog_0

Var long Cnt_Total

repeat

various code

Cog_1

dira[3]~~

repeat

if Cnt_Total == 5
outa[3]~~
else
outa[3]~

Cog_2

repeat

waitcnt(clkfreq/10 + cnt)

Cnt_Total++

If Cnt_Total == 10
Cnt_Total := 0

Comments

  • SarielSariel Posts: 182
    edited 2011-11-30 10:55
    I would use this object....

    http://obex.parallax.com/objects/25/
  • Mike GreenMike Green Posts: 23,101
    edited 2011-11-30 11:30
    1) With really simple cog use (no calling other objects, little use of local variables, few calls to other methods and calls not too deep), 20 longs may be enough most of the time. If there are problems or your use isn't simple, I'd start with 100 longs. Sariel's suggestion will help you find out actually how much stack space is used.

    2) If the cogs' code is all in one object (usually the main one), global variables are accessible from any cog. You do have to be careful about simultaneous access from more than one cog though. You're generally safe using a variable if you're modifying it only in one cog. If you've got an array or some group of variables that are related, you may get into trouble depending on the details of what you're doing. Some cases of this are ok like a circular buffer with an input pointer and an output pointer where one pointer is changed by one cog and the other pointer is changed by another cog. For other cases, you may need to allow only one cog at a time to access the variables using the LOCKxxx statements (see the Propeller Manual for details and examples).
  • Jorge PJorge P Posts: 385
    edited 2011-11-30 11:50
    For a detailed explanation of how to determine stack space be sure to view the video by Jeff Martin "How can you determine how much stack space is needed for Spin code launched into another cog?" He explains it all.
Sign In or Register to comment.