global information
sciguy
Posts: 48
What is the easiest way to make some variables (probably byte arrays) that can be accessed and written to by all the cogs? Do I have to pass everything through parameters, or is there a way to easily share a set of information?
Comments
The main problem you run into is that two cogs can also modify the same variables at the same time or one cog can access (read) a variable while another is modifying (write) it. There are ways around that, one of which is to use LOCKs so that only one cog can "get" the lock/semaphore at a time. The are ways to do this in some circumstances without using LOCKs, but you're limited to only one cog modifying the variable.
You just confused me Mike. You mean that if my main defines
rs[noparse][[/noparse] 3 ] : "FullDuplexSerial" 'Serial board to board communications
then does:
rc:= rs[noparse][[/noparse]g#navBoard].start(cNavRxpin, cNavTxpin, %0000, g#cBaud)
rc:= rs[noparse][[/noparse]g#motBoard].start(cMotRxpin, cMotTxpin, %0000, g#cBaud)
rc:= rs[noparse][[/noparse]g#senBoard].start(cSenRxpin, cSenTxpin, %0000, g#cBaud)
The variables defined in the VAR section are shared? I thought that I was getting three separate namespaces for each of the three instances started.
Am I screwed up here?
It's easy to imagine how multiple cogs can share the same object space in SPIN, because they all run from HUB memory. However, data encapsulation prevents data from being shared between objects unless they are passed with parameters or pointers.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
You're confusing "namespaces" with "instances". There's only one namespace for each object making up your program. By definition, this is the "space" of names recognized by / known to the compiler. On the other hand, in your example, there are 3 instances of the FullDuplexSerial namespace, one for each subscript value you've declared.
sciguy,
Yes. Each object that makes up a program has its own "namespace". All variables, constants, methods, etc. within that object are "known" throughout that object. There are some exceptions. For example, there are local variables within methods that are usable only within that method although curiously, you can't use the same name for a local variable as that of a global variable or constant. In some languages, you can reuse any name for a local variable. Go figure!
Constant names and public (PUB) method names are exported to other objects and can be referenced using "obj#constant" or "obj.method" notation where "obj" is the declared name of the object in the object referencing it.