AutoLocker Class Reference

#include <c4d_thread.h>

Detailed Description

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:
...
mutable AutoLock lock; // Note the keyword mutable
};
Definition: c4d_thread.h:222

In a member function:

void DriverHelper::FunctionXYZ()
{
AutoLocker al(lock);
// Do something
}
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:
AutoLock lock;
void FunctionA()
{
AutoLocker al(lock);
...
FunctionB(doc->GetFirstObject());
...
}
void FunctionB(BaseObject *op)
{
AutoLocker al(lock);
...
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.

Public Member Functions

 AutoLocker ()
 
 AutoLocker (AutoLocker &&src)
 
 AutoLocker (AutoLock &data)
 
 ~AutoLocker ()
 
void DoLock (AutoLock &data)
 
void Unlock ()
 

Private Member Functions

AutoLockeroperator= (const AutoLocker &d)
 
 AutoLocker (AutoLocker &data)
 

Private Attributes

maxon::Spinlockl
 
volatile UInt32ct
 

Constructor & Destructor Documentation

◆ AutoLocker() [1/4]

AutoLocker ( AutoLocker data)
private

◆ AutoLocker() [2/4]

Default constructor.

◆ AutoLocker() [3/4]

AutoLocker ( AutoLocker &&  src)

Copy constructor.

Since
R17.032
Parameters
[in]srcThe source AutoLocker.

◆ AutoLocker() [4/4]

AutoLocker ( AutoLock data)
explicit

Locks the passed AutoLock.

Parameters
[in]dataThe AutoLock to lock.

◆ ~AutoLocker()

~AutoLocker ( )

Destructor.

Member Function Documentation

◆ operator=()

AutoLocker& operator= ( const AutoLocker d)
private

◆ DoLock()

void DoLock ( AutoLock data)

Locks the passed AutoLock.

Parameters
[in]dataThe AutoLock to lock.

◆ Unlock()

void Unlock ( )

Unlocks the locked AutoLock.

Member Data Documentation

◆ l

maxon::Spinlock* l
private

◆ ct

volatile UInt32* ct
private