[MUD-Dev] Atomic functions

Ben bjchambers at phoenixdsl.com
Tue Nov 14 17:02:38 CET 2000


Felix A. Croes writes:
> An atomic function is a function which either completes entirely, or
> not at all.  An error in an atomic function rolls back changes made
> by that function.
>
>     object environment;
>
>     atomic void move(object destination)
>     {
> environment = destination;
> destination->enter_inventory(this_object());
>     }
>
> The move function above makes two changes.  First, it changes the
> environment of the current object.  Second, it places the current
> object in the inventory of the destination object.  However, the
> second step could fail because, for instance, there is no room in
> the inventory of the destination object.  Making the function atomic
> ensures that if the second step fails, the first step "never happened."
> There will still be an error to deal with, but at least that error has
> not left the object in an inconsistent state, with an environment
> which does not contain it.
>

Would it not be easier to check for the requirements first?
for example

void move(object destination)
{
if destination isn't valid or if destination->enter_inventory is not valid
    then don't do it
else proceed
}

or better yet, just attempt to place it in the inventory FIRST then change
the environment...

even better, set up the place it in inventory code to change the
environemnt, i.e

void enter_inventory(*this_object)
 {
    if space permits
        enter object
        *this_object->environment = destination
}

in this way, you save coding time, and also make it more efficient, no more
extra threads, or extra functions, just call the destination with the
object, instead of the object with the destination.





_______________________________________________
MUD-Dev mailing list
MUD-Dev at kanga.nu
https://www.kanga.nu/lists/listinfo/mud-dev



More information about the mud-dev-archive mailing list