AutoLocker Class Reference

#include <c4d_thread.h>

详细描述

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 { public : ... mutable AutoLock lock; // Note the keyword mutable };

In a member function:

void DriverHelper::FunctionXYZ() { AutoLocker al(lock);

// Do something }

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 ()); ... } };

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.

公共成员函数

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

私有成员函数

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

Private Attributes

maxon::Spinlock l
volatile UInt32 ct

构造函数 & 析构函数文档编制

◆  AutoLocker() [1/4]

AutoLocker ( AutoLocker data )
private

◆  AutoLocker() [2/4]

AutoLocker ( )

Default constructor.

◆  AutoLocker() [3/4]

AutoLocker ( AutoLocker &&  src )

Copy constructor.

由于
R17.032
参数
[in] src The source AutoLocker .

◆  AutoLocker() [4/4]

AutoLocker ( AutoLock data )
explicit

Locks the passed AutoLock .

参数
[in] data The AutoLock to lock.

◆  ~AutoLocker()

~ AutoLocker ( )

Destructor.

成员函数文档编制

◆  operator=()

AutoLocker & operator= ( const AutoLocker d )
private

◆  DoLock()

void DoLock ( AutoLock data )

Locks the passed AutoLock .

参数
[in] data The AutoLock to lock.

◆  Unlock()

void Unlock ( void  )

Unlocks the locked AutoLock .

Member Data Documentation

◆  l

maxon::Spinlock * l
private

◆  ct

volatile UInt32 * ct
private
BaseObject
定义: c4d_baseobject.h:224
AutoLocker
定义: c4d_thread.h:297
BaseObject::GetDown
BaseObject * GetDown(void)
定义: c4d_baseobject.h:274
BaseDocument::GetFirstObject
BaseObject * GetFirstObject(void)
AutoLock
定义: c4d_thread.h:219