Re: Global Variable Safety with Multi-tasking

new topic     » goto parent     » topic index » view thread      » older message » newer message

It sounds like you need to use a queue. Each task should be adding and removing objects to/from the queue and working on a local variable. This is a much safer way to prevent them from stomping all over each other.

sequence queue = {} 
 
procedure enqueue( object x ) 
 
    -- add an object into the queue 
    queue = append( queue, x ) 
 
end procedure 
 
function dequeue() 
-- (assume we've checked length(queue) elsewhere) 
 
    -- pull the first object from the queue 
    object x = queue[1] 
 
    -- remove this object from the queue 
    queue = queue[2..$] 
 
    -- return the object 
    return x 
end function 
 
procedure task_1() 
 
    while doing_stuff do 
 
        -- do stuff and queue an item 
 
        object x = some_function() 
        enqueue( x ) 
 
        task_yield() 
    end while 
 
end procedure 
 
procedure task_2() 
 
    while 1 do 
        -- loop forever 
 
        while length( queue ) = 0 do 
            -- wait until we have something to work on 
            task_yield() 
        end while 
 
        -- get the next object 
        object x = dequeue() 
 
        -- do something with this object 
 
        task_yield() 
    end while 
 
end procedure 

-Greg

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu