Hi,

thanks for your reply! I have just found out this problem. That will
be awesome, but how could I stop the callback? I mean the "Timer" wait
some time, but I want to interrupt it sometimes(with Cancel). So I
can't do it, because only one thread is available in JS and this is
used. But I'm sure it is possible somehow.
The main problem with it, If "Timer" had a private boolean and I could
modified another c thread, I wouldn't cancel the "SetTimeout" because
the wrapped class in v8 (and that time v8 is waiting), wasn't it?

My "cc Runner" solves this, there is a list of Tasks and Runner checks
in a loop how much time has taken in every Tasks. I want to take
advantage of it. I wanted to make Task to thread. But it crashes even
when only one thread is created :

extern "C" {
        static void* StartThread(void* task){
                Task* thread = static_cast<Task*>(task);
                thread->Start();
                return 0;
        }
}
pthread_t thread_;

void Task::Run(){

         pthread_create(&thread_, NULL, StartThread, this);
         printf("thread create\n");
}
void Task::Start(){

        int number;
//      v8::Locker l;
        v8::HandleScope scope;

        v8::Context::Scope context_scope(context_);

        v8::TryCatch try_catch;

        int counter = 1;
        v8::Handle<v8::String> s = function_->ToString();
        v8::String::AsciiValue ascii(s);
        std::cout<<"Script result : "<<*ascii<<std::endl;

// it crashes here

        v8::Handle<v8::Value> result = function_->Call(context_-
>Global(), 0, 0);
//      v8::Unlocker ul;
        if(result.IsEmpty()) {
                v8::Handle<v8::Value> exception = try_catch.Exception();
                v8::String::AsciiValue ascii(exception);
                std::cerr<<"Failed to run script in Task: "<<*ascii<<".\n";
        } else {
                number = result->Uint32Value();
                std::cout<<"Task result is good: "<<number<<std::endl;

        }

        printf("thread finished\n");
        pthread_join(thread_, NULL);

}


On máj. 13, 12:57, Matthias Ernst <[email protected]> wrote:
> 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......
>
> >> --
> >> ----- 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

Reply via email to