views:

30

answers:

3

In a custom queue, there's a push() and a pop() function. The pop() may be called any time on an Event.COMPLETE, so does this mean that code can be running in push() and pop() simultaneously? If so, is there a way to prevent the code from being run in both functions at the same time?

A: 

You need to google the concepts of an "atomic operation" in the context of "context switching", and also the idea of a mutex.

Basicly, a mutex is a boolean that gets set or cleared by your threads.

Each thread will:

function pop(){ Get_Mutex(); do_the_pop_operation(); Free_Mutex(); }

or

function push() { Get_Mutex(); do_the_push_operation(); Free_Mutex(); }

The Get_Mutex() function will sleep until it receives the mutex, thus only one operation can occur at a time.

asdlkf
I think the point here is less about locking and more about how Flash handles events. I'd imagine it uses a single thread and an even queue, but then again, I'm not a Flash expert. Does Flash really fire events from multiple threads?
Matti Virkkunen
Flash isn't multithreaded, but it seems to handle two different bits of code at the same time.
Roger B
+1  A: 

No, Flash is exclusively single threaded so there's no way two operations can happen at the same time.

Basically you won't have to worry about other code having not finished executing when handling an event.

James Hay
How does it handle events occurring at any time? Does Flash wait for the other code to finish executing before handling the event?
Roger B
@Roger B - See @shortstick link
James Hay
+1  A: 

you should read the post by senocular on this one (see Events and Frame Execution specifically) Order of operations.

although flash might give the impression multithreading with some asynchronious operations, it is only a single threaded program so you never need to worry about locking. basically the event operation is run "between" frames, so the code running "on" the frame is completed before/after the events are run, so there is no worry of overlapping functions.

shortstick