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.
公共成员函数 |
|
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 |
|
private |
AutoLocker | ( | ) |
Default constructor.
AutoLocker | ( | AutoLocker && | src | ) |
Copy constructor.
[in] | src | The source AutoLocker . |
|
explicit |
Locks the passed AutoLock .
[in] | data | The AutoLock to lock. |
~ AutoLocker | ( | ) |
Destructor.
|
private |
void DoLock | ( | AutoLock & | data | ) |
Locks the passed AutoLock .
[in] | data | The AutoLock to lock. |
void Unlock | ( | void | ) |
Unlocks the locked AutoLock .
|
private |
|
private |