About
Locks are used to guard access to resources that are shared across multiple threads.
- Note
- Locks should be used to avoid crashes when seldom used resources are accessed and to protect single threaded operations. Locks should not be used to fix problems of ill-designed code.
- Warning
- Make sure that a thread releases a lock after it has finished or a deadlock will be the result.
Spinlock
maxon::Spinlock implements a mutex that will loop on a pause/idle instruction when it is already locked.
 
ScopedLock
A maxon::ScopedLock acquires a lock when created and releases upon destruction (at the end of the scope).
- Warning
- ScopedLock should only be used for a short block of code. It will block all other threads.
 
{
 
  
 
  
  return UpdateGlobalData();
}
  
RWSpinlock
The maxon::RWSpinlock allows access for multiple readers and only one exclusive writer.
 
 
{
  
 
  
 
  
 
  return res;
}
 
{
  
 
  
 
  
 
  return res;
}
 
  
ARWLock
The maxon::ARWLock is an asymmetric read write lock which prefers the readers.
 
 
{
}
 
static void Clear()
{
}
 
 
{
 
 
 
  return res;
}
  
Synchronized
The maxon::Synchronized template is used to guarantee safe access to a given variable. It allows access to the variable only after a lock has been acquired. For asymmetric read-write access there is also maxon::RWSynchronized based on maxon::ARWLock.
 
class SimpleStringClass
{
public:
  
  {
    return *_string.Read();
  }
  
  {
    return _string.Write()->Append(string);
  }
  
  {
 
    
    auto lockedPtr = _string.Write();
 
    {
    }
 
    
  }
 
private:
};
  
Serializer
The maxon::Serializer guarantees mutually exclusive access to a shared resource. The given lambdas are enqueued and executed.
- Note
- maxon::Serializer is low level and does not support error handling.
 
      g_serializer.EnqueueAndWait([]()
        {
          iferr (UpdateGlobalData())
 
          {
          }
        });
 
  
Further Reading