Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use v8 in a thread?

I'm trying to use v8 from c++ inside a thread that isn't the main thread. There's no multi-threading as far as v8 is concerned, all v8 objects are created and destroyed within that thread. Nothing is running in parallel, nothing is being shared. When I run my program from the main thread, everything works fine. When I have the v8 stuff in another thread, I get segmentation fault when I create a v8::HandleScope.

I can't find any useful documentation on how threading is actually addressed with v8. The instruction "use isolates and lockers" pops up often when searching, but I can't find any examples on how this is done. There's this API doc on v8::Isolate, but nothing on that page tells me if I need them in my specific case (I'm not sharing memory or executing in parallel). The docs on v8::Locker() don't even have information about what the class is for. The included samples in the project don't deal with any of this either.

So my questions are...

  • Do I need to use isolates and/or lockers here?
  • Could I get a minimal example of how to use them? Even pseudo-code or something would be really useful
like image 240
Prismatic Avatar asked Aug 28 '12 06:08

Prismatic


1 Answers

You do need V8::Locker in the methods that will be working with the context when calling HandleScope. https://github.com/jasondelponte/go-v8/blob/master/src/v8context.cc#L41 is an example of how I've use the locker with v8. In this example it is used with multiple threads, but I believe the rule applies with single threads also.

Isolates are only needed when you want multiple instances of v8 in parallel.

https://groups.google.com/forum/?fromgroups=#!topic/v8-users/FXpeTYuAqKI Is an old thread I found a bit ago that helped me solve my problem with the library crashing as soon as HandleScope local variable was created.

like image 180
Jason D. Avatar answered Sep 29 '22 04:09

Jason D.