[OpenLayers-Dev] threading/synchronicity in javascript

Erik Uzureau euzuro at gmail.com
Tue Nov 4 16:49:45 EST 2008


Tim and Everyone else that responded to this thread: THANKS!


I think tim hit is on the button with this:

"functions like setTimeout() and asynchronous callbacks need to wait for the
script engine to sleep before they're able to run."

I have been futzing around with a sort of wrapper for making JSON requests
and just needed to ensure that a certain section
of code was 'atomic'. Sounds like as long as i'm not calling alert() in the
middle of it, nothing to worry about: phew!

Interesting also to note the possibilities with threading in HTML5. Sounds
like things are going to get *complicated*.

Anyways, thanks!

Erik

On Mon, Nov 3, 2008 at 10:40 PM, Tim Schaub <tschaub at opengeo.org> wrote:

> Hey-
>
> Erik Uzureau wrote:
> > Dear OpenLayers users & dev,
> >
> > Pardon the non-geo post, but I am wondering if anyone out there has come
> > up across issues with
> > synchronicity in javascript?
> >
> > After pouring over this stuff for several hours, I think I am pretty
> > resolved to just say "ok, javascript
> > is event-driven and not truly multithreaded, so there is no need to
> worry".
> >
> > In the off-chance that someone out there might have information
> > otherwise, please let me know.
>
> I think the clearest answer is that event listeners and "functions like
> setTimeout() and asynchronous callbacks need to wait for the script
> engine to sleep before they're able to run."
>
> Maybe you're concerned about something else, but if you're wondering
> whether a sequence of event handling code can be executed again before
> it has finished, the answer is no.
>
> Again, maybe you're reading up on mutexes for something else, but here's
> some code that demonstrates that you can't change the "current" value
> with subsequent triggers of the same event while the event handling code
> is executing, no matter how long it takes to run.
>
> function check(iter) {
>     var current;
>     document.body.onmousemove = function() {
>         var start = (new Date).getTime();
>         var id = start + Math.random();
>         current = id;
>         // do something that takes a while
>         var a = [];
>         for(var i=0; i<iter; ++i) {
>             a.push(i);
>         }
>         var end = (new Date).getTime();
>         console.log(
>             id + " start: " + start + " end: " + end +
>             " elapsed: " + (end - start)
>         );
>         if(current != id) {
>             console.error(id + " failed");
>         }
>     }
> }
>
> // loop runs in <1ms, events fired every ~17ms
> check(1000);  // wiggle mouse over body
>
> // loop runs in ~25ms, events fired every ~45ms
> check(100000);
>
> // loop runs in ~225ms, events fired every ~250ms
> check(100000);
>
> You can do the same sort of thing with setTimeout.
>
> Tim
>
> >
> > Below are two of the links that have fed my brain for the last few hours:
> >
> > debate:
> >
> http://stackoverflow.com/questions/124764/are-mutexes-needed-in-javascript
> >
> > implementation of a mutex in js:
> > http://www.developer.com/lang/jscript/article.php/3592016
> > (note that his actual code for this *only* runs in ie, which obviously
> > docks it a fair amount of credit)
> >
> > cheers,
> > erik
> >
> >
> > ------------------------------------------------------------------------
> >
> > _______________________________________________
> > Dev mailing list
> > Dev at openlayers.org
> > http://openlayers.org/mailman/listinfo/dev
>
>
> --
> Tim Schaub
> OpenGeo - http://opengeo.org
> Expert service straight from the developers.
> _______________________________________________
> Dev mailing list
> Dev at openlayers.org
> http://openlayers.org/mailman/listinfo/dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.osgeo.org/pipermail/openlayers-dev/attachments/20081104/ae480c5d/attachment.html


More information about the Dev mailing list