Shop OBEX P1 Docs P2 Docs Learn Events
Looking for an example of lock useage — Parallax Forums

Looking for an example of lock useage

krazyideaskrazyideas Posts: 119
edited 2009-06-24 11:01 in Propeller 1
Hi guys.

I have a RPM gage that I am trying to build.
I got it all working, but I get some crazy number inttermittly. I did some looking around and I now figure that it is because I am trying to read a varuble with my display cog that is in the mist of being written by my monitoring cog.

Just trying to learn to use two cogs, These locks seem like they will be handy.
If someone can point me to some good examples and or explanations I would appreceate it.

Thanks from a amature

Comments

  • pgbpsupgbpsu Posts: 460
    edited 2009-06-23 20:04
    I've been looking at this myself recently. The manual has some examples that can provide an idea of how to use them. Have a look at pages 228-235 (in the printed version). If I had a working example I'd share it with you. Unfortunately I don't. Sorry. But from reading over the Prop manual, it looks like most of what you need is there.

    I should have time to return to this next week. If you haven't worked this out by then, maybe I can help more.

    Your symptoms should like a good candidate for this.

    Good luck.
  • stevenmess2004stevenmess2004 Posts: 1,102
    edited 2009-06-24 09:35
    krazyideas said...
    I got it all working, but I get some crazy number inttermittly. I did some looking around and I now figure that it is because I am trying to read a varuble with my display cog that is in the mist of being written by my monitoring cog.
    Is the variable only one byte/word/long or a string? If it's only one byte/word/long then you shouldn't need a lock if you are only reading it because each cog accesses the hub at a different time. If it's a string then a lock may or may not be necessary depending on how long it takes to read and write the string in the different cogs. You only need the locks when two pieces of code want to write to the same memory location.
  • mctriviamctrivia Posts: 3,772
    edited 2009-06-24 10:32
    here is an object I wrote to run my 60x14 led display. Uses locks in both assembly and spin.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    propmod_us and propmod_1x1 are in stock. Only $30. PCB available for $5

    Want to make projects and have Gadget Gangster sell them for you? propmod-us_ps_sd and propmod-1x1 are now available for use in your Gadget Gangster Projects.

    Need to upload large images or movies for use in the forum. you can do so at uploader.propmodule.com for free.
  • heaterheater Posts: 3,370
    edited 2009-06-24 11:01
    As you may have worked out already locks are not always required when sharing data between COGs (or other types of concurrent process).

    For example, if there is only one COG writing a string or complicated data structure and only one COG reading said data then a simple flag in HUB will do.

    Basically the writer waits in a loop looking for the flag to be 0 whenever he wants to write data.
    When he sees the zero he can write the data and set the flag to 1.

    The reader COG waits in a loop for the flag to become 1 whenever he wants to read the data.
    When he sees the 1 he reads the data and set the flag back to 0.

    This can be extended to one writer and many readers with a flag for each reader but at this point locks may be more convenient.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    For me, the past is not over yet.
Sign In or Register to comment.