#include <v8.h>
Public メソッド | |
Unlocker () | |
~Unlocker () |
Multiple threads in V8 are allowed, but only one thread at a time is allowed to use V8. The definition of 'using V8' includes accessing handles or holding onto object pointers obtained from V8 handles. It is up to the user of V8 to ensure (perhaps with locking) that this constraint is not violated.
If you wish to start using V8 in a thread you can do this by constructing a v8::Locker object. After the code using V8 has completed for the current thread you can call the destructor. This can be combined with C++ scope-based construction as follows:
... { v8::Locker locker; ... // Code using V8 goes here. ... } // Destructor called here
If you wish to stop using V8 in a thread A you can do this by either by destroying the v8::Locker object as above or by constructing a v8::Unlocker object:
{ v8::Unlocker unlocker; ... // Code not using V8 goes here while V8 can run in another thread. ... } // Destructor called here.
The Unlocker object is intended for use in a long-running callback from V8, where you want to release the V8 lock for other threads to use.
The v8::Locker is a recursive lock. That is, you can lock more than once in a given thread. This can be useful if you have code that can be called either from code that holds the lock or from code that does not. The Unlocker is not recursive so you can not have several Unlockers on the stack at once, and you can not use an Unlocker in a thread that is not inside a Locker's scope.
An unlocker will unlock several lockers if it has to and reinstate the correct depth of locking on its destruction. eg.:
// V8 not locked. { v8::Locker locker; // V8 locked. { v8::Locker another_locker; // V8 still locked (2 levels). { v8::Unlocker unlocker; // V8 not locked. } // V8 locked again (2 levels). } // V8 still locked (1 level). } // V8 Now no longer locked.
v8::Unlocker::Unlocker | ( | ) |
v8::Unlocker::~Unlocker | ( | ) |