Shop OBEX P1 Docs P2 Docs Learn Events
Sharing a value between different cogs — Parallax Forums

Sharing a value between different cogs

jbalatjbalat Posts: 96
edited 2009-12-03 14:35 in Propeller 1
What is the go with sharing a global variable ?

If I read a frequency and then store it into a long variable on one cog, can I access it at any time from another cog or the main method or do I need to lock and unlock functions ?


My understanding is that Writing of a long variable may take more than one hub turn.. so if you try to read it from another cog it may be corrupted if the whole value is not written to memory

Please explain...

Comments

  • BeanBean Posts: 8,129
    edited 2009-12-03 03:48
    Variable in PASM cogs cannot be accessed by other cogs. You need to write the value to a hub location.

    A single long in the hub will be updated in one hub turn. You only need to use the locks if you are updating a multi-long structure then you need to use the locks.

    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    1st rule of finance...Don't lose money.
    Never be ashamed of making an honest·profit.
    Live within your means...Make do, or do without.
    ·
  • MagIO2MagIO2 Posts: 2,243
    edited 2009-12-03 07:04
    "A single long in the hub will be updated in one hub turn. " Guess that could be missinterpreted as "HUB needs a full turn to write a long". The value in HUB will be updated in the COGs access window - which is 2 instruction cycles. After that the next COG has it's access window. And this COG will already have access to exactly that written data. So, there is no chance to read corrupted longs - Except you read the long by using the rdbyte instructions ;o)

    As one full HUB turn is 16 instruction cycles, 8 longs can be written to HUB-RAM in one turn if all COGs want to write something.
  • jbalatjbalat Posts: 96
    edited 2009-12-03 11:00
    So talking spin... when you mean write the variable to the Hub you just declare it in the VAR section at the top of your program?..

    Any cog can then reference or modify it safely without using the "@" modifier
  • MagIO2MagIO2 Posts: 2,243
    edited 2009-12-03 14:10
    Talking SPIN: yes, each SPIN COG which has been started in the same SPIN-file has access to that variable and can safely modify. VARs are located in HUB-RAM. In SPIN you don't have access to COG-RAM.

    If you talk PASM, things get more complex. But that's a lesson to learn in future ;o)
  • BeanBean Posts: 8,129
    edited 2009-12-03 14:35
    Oh, sorry. I assumed you were talking about PASM code.
    I didn't mean to confuse the issue.

    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    1st rule of finance...Don't lose money.
    Never be ashamed of making an honest·profit.
    Live within your means...Make do, or do without.
    ·
Sign In or Register to comment.