A class for thread-safe code access within its current scope.
please be aware that this is depreciated.
Example:
Embed AutoLock in a class that needs to be protected.
class DriverHelper
{
public:
...
};
Definition: c4d_thread.h:222
In a member function:
void DriverHelper::FunctionXYZ()
{
}
Definition: c4d_thread.h:300
AutoLocker guarantees that all code within the member function can be accessed thread-safe as no other thread can have access at the same time.
This does not solve the general problem of deadlocks that can occur if the calls are done cross-wise, meaning calls between threads.
AutoLocker has the advantage over Spinlock that calls of subroutines with the same protection will not get a deadlock.
Also, AutoLocker will automatically unlock everything, so there are no missing Unlock() calls (can be tricky if the code returns at multiple places otherwise).
Another example of recursive or deep calls with the same AutoLock:
class ClassA
{
public:
void FunctionA()
{
...
FunctionB(
doc->GetFirstObject());
...
}
{
...
FunctionB(
op->GetDown());
...
}
};
Definition: c4d_baseobject.h:225
const char * doc
Definition: pyerrors.h:226
PyObject * op
Definition: object.h:520
Both FunctionB()
calls are fine and will not cause deadlocks. It is important that the lock is performed on the same AutoLock otherwise you will run into deadlocks.
- Note
- Should be used for fast methods, for instance to serialize short data access.
As an example within normal objects or GUI managers where routines are called by maybe 1 or 2 threads. In an object for instance one for the viewport and one for generation/execution and maybe a message from another thread.
Should be avoided for rendering though where locking to access one thread will seriously impair speed.