JS is a single-threaded language that doesn't know preemption. A piece of JS code will always run to end uninterrupted. A timer callback or event handler can only run when the current one gives up. You're setting yourself up for more trouble than it's worth if you introduce threads into the picture. Using threads to implement timers only puts a burden on you to do locking when you don't actually need it.
Avoid it in the first place. If you admit to yourself that only one piece of JS can only ever execute at a time, you can implement this simply using a priority queue and a single-threaded C++ main loop like this: priority_queue timers; // using timeout as ordering export JS "Timer" that pushes (timeout, Persistent<Function> callback) to "timers". run initial script // sets up timers, done. while !timers.empty(): const Timer& timer = timers.top(); sleep until timer expires timer.callback->Call(); // may schedule new timers timers.pop(); // calls ~Timer calls callback.Dispose() This is nothing different from what node does, except from that "timers" is "timers and io interests" and "sleep" is "select with timeout". On Fri, May 13, 2011 at 9:38 AM, kodq <[email protected]> wrote: > Hi, > > Thanks for your help. It was very useful. > > Bye d. > > On máj. 13, 01:12, Stephan Beal <[email protected]> wrote: >> On Fri, May 13, 2011 at 12:01 AM, Louis Santillan <[email protected]>wrote: >> >> > You can find an implementation & discussion of the browser's setTimeout >> > here: >> >http://code.google.com/p/v8-juice/wiki/ThreadingInJS >> >http://code.google.com/p/v8-juice/wiki/BindableFunctions >> >> >http://code.google.com/p/v8-juice/source/browse/branches/edge/src/cli... >> >> Thanks for the plug :). >> >> The related code is actually independent of the v8-juice library (despite >> being in the same namespace) and can easily be dropped in to arbitrary v8 >> clients (but you may need to re-set a #define or two in the cc file): >> >> http://code.google.com/p/v8-juice/source/browse/trunk/src/lib/juice/t...http://code.google.com/p/v8-juice/source/browse/trunk/src/include/v8/... >> >> -- >> ----- stephan bealhttp://wanderinghorse.net/home/stephan/ > > -- > v8-users mailing list > [email protected] > http://groups.google.com/group/v8-users -- v8-users mailing list [email protected] http://groups.google.com/group/v8-users
