Shop OBEX P1 Docs P2 Docs Learn Events
2 cogs running the same object — Parallax Forums

2 cogs running the same object

william chanwilliam chan Posts: 1,326
edited 2008-03-04 00:01 in Propeller 1
Hi,

1. If 2 cogs runs different methods of one object, and both cogs actively changes values of global variables in that object,
would we need semaphores to prevent program crashes?

2. What is the safest stack size value that you would recommend to beginners?

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
www.fd.com.my
www.mercedes.com.my

Comments

  • stevenmess2004stevenmess2004 Posts: 1,102
    edited 2008-03-03 10:31
    1. This depends on your variable. If it is a single byte, word or long than you don't need to worry about semaphores/locks. If it you are using more than one byte/word/long than you may need to use a semaphore/lock depending on how your program is written.

    2. As big as you can make it smile.gif This really depends on the complexity or your program. Try starting at 20 to 30. This is really the minimum. If weird things start happening than make this bigger. It wouldn't be anything unusual to need a stack of at least 50 to 100 longs.
  • mparkmpark Posts: 1,305
    edited 2008-03-03 20:32
    I don't see what the variable's size has to do with it. You need some sort of lock mechanism to prevent race conditions.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Michael Park

    PS, BTW, and FYI:
    To search the forum, use search.parallax.com (do not use the Search button).
    Check out the Propeller Wiki: propeller.wikispaces.com/
  • Mike GreenMike Green Posts: 23,101
    edited 2008-03-03 20:44
    You don't always have to use a lock mechanism. Byte, word, and long fetches and stores are atomic, so there's no problem of a long being partially fetched while another cog updates one of the bytes of the long value.

    There a special case of multiprocessor operation where only one processor writes to a shared value, but multiple processors can read it. A FIFO buffer is one case of this where one processor is a producer of values and another processor is a consumer of values. This can be implemented as two single producer / single consumer variables, the buffer pointer used for writing to the buffer and the pointer used for reading from the buffer. A lock is not necessary for this mechanism.
  • mparkmpark Posts: 1,305
    edited 2008-03-04 00:01
    Reads and writes are atomic, but I read the original question as being about read-modify-write. I might be reading more into it than is actually there, though.



    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Michael Park

    PS, BTW, and FYI:
    To search the forum, use search.parallax.com (do not use the Search button).
    Check out the Propeller Wiki: propeller.wikispaces.com/
Sign In or Register to comment.