PDA

View Full Version : Synchronization problem in AS3



spiderbox.am
December 20th, 2007, 08:05 AM
Hi everybody.
I have one problem with synchronization of write functions...
The problem:
There exist socket A.
There are many event handlers in my program which can directly write bytearrays to socket A. So the problem is how can I do the synchronization or are sockets thread safe ?
If 2 functions(handlers) will start to write into the same socket what will happen ?
This is a very important problem if you are Flex/Flash programmer/developer.
Thanks ever so.

spiderbox.am
December 21st, 2007, 05:07 AM
After reading several materials in the web and doing experiments in ActionScript3 I can made the following conclusion:
1. Flash player (AS2,AS3) is not multithreaded.
2. If 2 functions are being called in the same time , only one will be executed and after the function returns the second one will be called.

To ensure the second point here is the example:
function func1(){
trace("aStarted")
for(var i:uint=0;i<10000;++i);
trace("aFinished")
}

function func2(){
trace("bStarted")
for(var i:uint=0;i<1000;++i);
trace("bFinished")

}

setInterval(func1,100);
setInterval(func2,10);

In this example you cannot see message like this in the output window:
aStarted
bStarted
or
bStarted
aStarted

As you can see func1 is working 10 times longer
and func2 is called 10 times faster but even in this situation func2 will not be executed while func1 is working.

If you have something to add or you thing that my conclusion is not true than please let me know.

Thanks