Shop OBEX P1 Docs P2 Docs Learn Events
Issue with multiple cogs accessing a common PUB? — Parallax Forums

Issue with multiple cogs accessing a common PUB?

DiverBobDiverBob Posts: 1,110
edited 2014-03-13 03:32 in Propeller 1
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

Comments

  • kuronekokuroneko Posts: 3,623
    edited 2014-03-12 19:19
    Most likely reason for failure here is that the common method simply can't cope with concurrent calls. As always, it would help immensely if you'd post some code demonstrating the issue.

    Also, only another copy of the SPIN interpreter is loaded into the new cog. Methods are executed in its context.
  • DiverBobDiverBob Posts: 1,110
    edited 2014-03-13 03:05
    I would have posted the code but it is rather large and complex. Plus the last time I posted a portion of the code I caused Johnny Mac's eyes to bleed (it's gotten worse since then!) and I didn't want to do that again!

    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.
  • Heater.Heater. Posts: 21,230
    edited 2014-03-13 03:24
    DiverBob,

    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.
  • Heater.Heater. Posts: 21,230
    edited 2014-03-13 03:32
    Of course a simple way to run the same code in many COGS is to put that code into it's own object. Then use many instances of that object. Any VAR variables will be instanciated separately for each instance. DAT data is shared between instances.
    OBJ
    concurrent1 : "concurrent.spin"
    concurrent2 : "concurrent.spin"
    
    PUB main
        concurrent1.start
        concurrent2.start
    
    You then just have to think about how you are going to get data in and out of those instances.
Sign In or Register to comment.