# Mutual Exclusion

Ian Macintosh (ian@iman.internet.co.nz)
Wed, 8 Nov 1995 04:39:20 PST

- Messages sorted by:[ date ][ thread ][ subject ][ author ]

    On Tue, 7 Nov 1995, CJ Holmes wrote:

    > Is there a way to gurantee that only one task can access a variable at a
    > given time?
    >
    > I have a financial-flow simulation where hundreds of robotic "consumers"
    > are engaging in various transactions.  I suspect I'm getting into trouble
    > when I check an account to see if it has the required sum of currency
    > withing, and then subtract from it (in the case of a debit).  What if
    > another task modifies the account between those two statements?  Is there a
    > way I can "lock" the variable in question?

    That is already guaranteed with the design of LambdaMOO.  It is a
    non-preemptive multitasking operating system, much like Windows 3.11 and
    predecessors.

    What that really means is that the only way you can have a problem is if
    you design your code wrong.

    ie.  Wrong design:

    Variable        Current Balance (CB)    1000

    Robot A         Reads CB (1000)

    you suspend()

    Robot B         Reads CB (1000)
    Robot B         Deducts 400
    Robot B         Writes CB (600)
    task ends

    Robot A         Deducts 100
    Robot A         Writes CB (900)
    task ends

    Basically, all you need to ensure is that you do not suspend between
    reading the variable, and writing to the variable, and all is well :^)

    Regards,

    Ian.

    +-------------------------------------------------------------------------+
    | Ian Macintosh         | P.O. Box 24-036      | Anything really worth    |
    | General Manager       | Royal Oak            | doing, is worth doing    |
    | Sytek New Zealand Ltd | Auckland 1           | badly.  - Duncan Shaw    |
    | Tel : +64 9 480-3034  |                      | Mobile : +64 25 836-230  |
    +-------------------------------------------------------------------------+