10 Replies Latest reply on Sep 2, 2007 11:37 PM by kglad

    Does AS2 support concurrent programming

    sneakyimp Level 2
      It just ocurred to me last night that my current flash project could suffer from concurrent execution problems. for instance, two different movies try to check and modify the same global variable. If the function that checks/modifies this global array gets interrupted then I could get some really bizarre results.

      does actionscript 2 have any language support for disabling interrupts? are there any resources that describe flash's multithreading behavior? is it really multithreaded?
        • 1. Re: Does AS2 support concurrent programming
          kglad Adobe Community Professional & MVP
          no multi-threading. code executes sequentially (except of a handful of asynchronous functions, like load functions).
          • 2. Re: Does AS2 support concurrent programming
            sneakyimp Level 2
            hi kglad! i'm always honored when you respond.

            Flash definitely has at least has some quasi-multithreading going on...maybe it's not true multithreading (as in it capitalizes on a quad-core cpu) but it does exhibit some semblance of multithreading behavior.

            for instance, suppose I spawn ten movie clips on the first frame of my flash project, each of which contacts a different webserver and handles the results with an onLoad event. While the execution of the outgoing requests might be predictable (or is it?), somebody somewhere is multithreading to handle the synchronization required to get all those responses back into my flash movie. I have no guarantees about the sequence in which the onLoad events will be executed and I'm not even sure that one particular onLoad event won't be interrupted to start another onLoad event. see what i mean? What level of granularity can I expect from Flash code execution? Are there any hard-and-fast rules about when code is interrupted and when it's not?

            I was hoping to learn a bit more about how Flash simultes this concurrent execution.
            • 3. Re: Does AS2 support concurrent programming
              kglad Adobe Community Professional & MVP
              like i said, load functions are asynchronous. anything being retrieved outside the swf is going to be available asynchronously. everything else is synchronous.

              in particular, load requests are synchronous. the onLoad functions are going to execute asynchronously.

              a.s. code is not interrupted (except when it exceeds the time limit setting or flash detects 256 indentical loops.
              • 4. Re: Does AS2 support concurrent programming
                sneakyimp Level 2
                THANKS for the answer!

                How do you *know* this stuff kglad? Is there anywhere this is documented in more detail? I want to make sure my project doesn't have problems and I think I need a little more detail.

                • 5. Re: Does AS2 support concurrent programming
                  kglad Adobe Community Professional & MVP
                  i don't know if it's documented. it probably is, but i don't recall ever reading about it.

                  i've learned about most a.s. code being synchronous by testing in flash. i've learned about load results being asynchronous by thinking they were synchronous (actually, i wasn't thinking at all) and having problems using or referencing assets that weren't available.
                  • 6. Does AS2 support concurrent programming
                    sneakyimp Level 2
                    So I've been trying to construct a test script to hopefully understand this better but I'm having a bit of a problem. I made a loop to create 10 loadvars objects loading each from a different website. I was hoping to make the 10 onLoad functions for each loadVars identify itself when it runs but all 10 loadVars objects are pointing to the exact same memory location rather than the value of i when they were created so I cannot distinguish the different loadVars objects...my code is attached.

                    EDIT: reformatted code to fix tab/spaces issue
                    • 7. Re: Does AS2 support concurrent programming
                      sneakyimp Level 2
                      OK i got it fixed. I think this code pretty clearly illustrates just how sequential flash code execution is. Not a single onLoad event runs until all of the loadVars objects have been created even though the main loop spends A LOT of time doing nothing. I'm pretty sure most of the loadvars objects are loaded by the time the main loop completes and Flash still makes them wait til the main loop finishes.

                      I'm still wondering if there is any chance for race conditions or deadlock or any other weirdness in my code but I have a much better idea of how things work.


                      • 8. Re: Does AS2 support concurrent programming
                        kglad Adobe Community Professional & MVP
                        for-loops execute from beginning to end and cannot be interrupted, once they start, by anything else that occurs in a swf.
                        • 9. Re: Does AS2 support concurrent programming
                          sneakyimp Level 2
                          That's really useful info! I'm guessing that a function won't be interrupted either. Adobe should have docs on this somewhere. If they don't, well that's just *shameful*.

                          • 10. Re: Does AS2 support concurrent programming
                            kglad Adobe Community Professional & MVP
                            a function's block of code is no different than any other lines of code: it executes synchronously, except for load functions.