views:

80

answers:

1

I have a flash application using callbacks to javascript functions (eg. when it receives some data over a socket, it'll call a js script which would change the content of a div according to that given data).

Afaik, there is no actual mutual exclusion in javascript so I'm not sure if I can/need to simulate something like :

callbackFunc() {
lock(mutex1)
foo
unlock(mutex1)
}
...
someOtherFunc() {
lock(mutex1)
bar
unlock(mutex1)
}

So, the question is, when are those callbacks called ? Are they simply queued to be executed right after the browser finishes its task or are they triggered randomly ?

A: 

After a few hours of research, I read this page where I saw a question pretty close to what I had asked.

What I wanted to see was what would happen if the socket receives data faster than the browser-side script handles it (for instance, receiving 2 requests per second after receiving which browser should show and hide a gif animation for half a second without interrupting it) and if they try to modify the same array, so I did a few tests.

In case of multiple functions inserting values into an array and a single one popping them (and clearing their intervals when there is too much data in the said array), it worked as if the browser uses a fifo method to handle when to execute those functions (I don't know if different browsers handle this in different ways, I used Firefox and Opera) : They won't interrupt the function which is being executed.

Also, when there were two or more functions to be called at the same time (with setTimeout(...,1000), for example), there is no way to tell which one will be called next, so I used some simple variables (like a boolean) to synchronize them.

In short, when there are possible external callbacks, they won't corrupt a global object used by another function, but there has to be a value mimicing a semaphore if synchronization is what you need.

felace