Codemesh Runtime v3 C++ API Reference
3.9.205
|
A utility class used for synchronizing access to a Java object. More...
#include <xmog_lock_holder.h>
Public Member Functions | |
xmog_lock_holder (xmog_java_ref &_toLock) | |
Attempts to lock the given proxy object on the Java side. | |
~xmog_lock_holder () | |
Unlocks the proxy object on the Java side. | |
A utility class used for synchronizing access to a Java object.
In Java, you can write thread-safe code by protecting access to code blocks with the synchronized
keyword, for example:
In C++, you could directly use the xmog_java_ref.xmog_monitor_enter or xmog_java_ref.xmog_monitor_exit methods, but the xmog_lock_holder utility class is much more convenient.
The above code translates into C++ as follows:
The scope defined by the parentheses defines the lifetime of the xmog_lock_holder instance. The constructor invokes the xmog_monitor_enter() method and the destructor invokes the xmog_monitor_exit() method.