Shop OBEX P1 Docs P2 Docs Learn Events
sharing objects among cogs -- I want an object singleton — Parallax Forums

sharing objects among cogs -- I want an object singleton

TinkersALotTinkersALot Posts: 535
edited 2013-04-13 14:03 in Propeller 1
How can I pass an object created in cog 0 into other objects running in other cogs?

Here is more on the scenario: In cog zero I create a random number generator (uses a cog) and then 4 other objects that run in their own cogs. I would like to use the single random number generator in all the other cogs, but I am having trouble finding a sample of this.

Is it possible? Do I pass the address of the object I want to share? How is the object "declared as a pointer" in the other objects (I don't think I want to declare anohter random number generator in the other cog objects do I? If it is, can someone help direct me to documentation that illustrates how this is done?

Comments

  • Heater.Heater. Posts: 21,230
    edited 2013-04-13 13:34
    First off. Objects and COGs have pretty much nothing to do with each other. They are orthogonal concepts.
    A cog can be running a object which in turn calls methods on sub objects and sub sub objects etc.
    Conversely a method of an object can be run by many cogs at the same time.
    Then a gain a single object can start multiple cogs. And so on.

    There should be a big bold description of this somwhere because it is a confusion that arises all the time.

    What you are wanting to do is pass a refence to an object or an objects method to other objects or methods. There is no way to do that.

    What yo can do is set a mailbox, a little area of RAM, where commands and results are exchanged between an object and those whishing to use it's services. Then pass the address of that mailbox around between parent and child objects so that the children can use the services. Of course if many client objects are using such a service you may need locks around the mail box to prevent datat corruption.

    In the case of your random number generator though perhaps there is an easier way. If it is only writing random numbers to a long as fast as possible.

    You can instantiate that random number generator many times in many child objects (in their obj sections). The code will only be included in the program once. Have the random number generator write its numbers to a long in a DAT section. There will only be one DAT section, only VAR is created for each new instance. Have the parent (only) call the start method on the random number object that does the cog new. All children will be able to read the random data from the long they see in their view of the random object.
  • TinkersALotTinkersALot Posts: 535
    edited 2013-04-13 13:46
    Thank you, Heater for your reply. I did find a solution -- and it works pretty well.

    The object here offers a solution to what I need:
    http://obex.parallax.com/objects/189/

    which is discussed here:
    http://forums.parallax.com/showthread.php/94311-SerialMirror-A-FullDuplexSerial-enhancement?p=649541#post649541

    and is offered as a solution to "the same question" that was posted here as question #2:
    http://forums.parallax.com/showthread.php/92551-Spin-Objects

    The modification to my random number object is trival. move the "local vars" from "var" to "dat" space and add a lock into the object for "cog safety" because multiple cogs will be requesting random numbers. This allows me to start the object once from cog zero and then objects that are running in other cogs can use the same random number generator .

    NEAT.

    what a great site this is.
  • Heater.Heater. Posts: 21,230
    edited 2013-04-13 13:55
    How are you generating your random numbers? With Chip's real random object.
    If you need to save a COG this random number generator may help:http://forums.parallax.com/showthread.php/136975-JKISS32-High-quality-PRNG-with-RealRandom-seed-save-a-COG.
  • TinkersALotTinkersALot Posts: 535
    edited 2013-04-13 14:03
    Heater. wrote: »
    How are you generating your random numbers? With Chip's real random object.
    COG.[/URL]

    Yes -- that is what I started with. It has been modified some now though to be "cog safe singleton" :)

    Thank you for the ohter link. I will check it out straight away.
Sign In or Register to comment.