Issue with multiple cogs accessing a common PUB?
DiverBob
Posts: 1,110
Time for my next question, I've got 3 cogs that run 3 seperate routines in a repeating loop. Each of those routines independently calls the same PUB routine. I've noticed that I'm getting some bad return values from the common routine which makes me believe there might be some problem doing this. In testing when only one cog is active the program works without any problems, only when I start up the other cogs does the problem exist.
When starting a new cog I know the routine in the cognew statement is loaded into the new cog, but what about any other routines that are called by the original routine, are they also loaded in the cog?
I'm missing something here and could use some help. Thanks
Bob
When starting a new cog I know the routine in the cognew statement is loaded into the new cog, but what about any other routines that are called by the original routine, are they also loaded in the cog?
I'm missing something here and could use some help. Thanks
Bob
Comments
Also, only another copy of the SPIN interpreter is loaded into the new cog. Methods are executed in its context.
Through testing I came to the conclusion in the original post and last night found that by making three copies of the shared routine, each with unique names so that each cog only called its own routine, the problem went away. Obviously that isn't a very efficient use of code space so I was wondering if there is some method I wasn't aware of that could allow a single routine to be called by three cogs properly.
It's not the sharing of code between COGS that is the problem. Code is just a static pile of Spin byte codes the interpreter in the COG reads and executes.
It's the data those methods use that is the problem. Imagine: One COG is updating vars A,B.C in some way that it likes. Meanwhile some other COG is updating the same vars A,B,C at the same time. The result is chaos in that data. Like when many people are speaking at once. No body understands anything.
If you have data structures that both COGS need to update you need to control access to that data so that only one COG has access at a time. The other has to wait until the first is finished. Look in the manual for "locks" to see how to do this. (It can be done without locks in some cases but that's another story)
If the COGs are not supposed to be sharing data then ensure they do not. Perhaps by passing a pointer (the address) of that data into the PUB method such that each PUB operates on different data.