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.
Embed AutoLock in a class that needs to be protected.

class DriverHelper
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
AutoLock lock;
void FunctionA()
AutoLocker al(lock);
void FunctionB(BaseObject *op)
AutoLocker al(lock);
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.

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 (void)

Private Member Functions

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

Private Attributes

volatile UInt32ct

Constructor & Destructor Documentation

◆ AutoLocker() [1/4]

AutoLocker ( AutoLocker data)

◆ AutoLocker() [2/4]

Default constructor.

◆ AutoLocker() [3/4]

AutoLocker ( AutoLocker &&  src)

Copy constructor.

[in]srcThe source AutoLocker.

◆ AutoLocker() [4/4]

AutoLocker ( AutoLock data)

Locks the passed AutoLock.

[in]dataThe AutoLock to lock.

◆ ~AutoLocker()

~AutoLocker ( )


Member Function Documentation

◆ operator=()

AutoLocker& operator= ( const AutoLocker d)

◆ DoLock()

void DoLock ( AutoLock data)

Locks the passed AutoLock.

[in]dataThe AutoLock to lock.

◆ Unlock()

void Unlock ( void  )

Unlocks the locked AutoLock.

Member Data Documentation

◆ l

maxon::Spinlock* l

◆ ct

volatile UInt32* ct