Skip to content

Commit

Permalink
Simultaneous acquisition of locks avoids possible deadlock.
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel Hare authored and codemercenary committed Aug 5, 2014
1 parent a340c9d commit 2014716
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions autowiring/atomic_object.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class atomic_object {
/// atomic_object<object> target(*unlock_object<object>(source));
///</remarks>
atomic_object(const atomic_object<object>& source) {
std::lock_guard<lock> lock_this(source.m_lock);
std::lock_guard<lock> lock_source(source.m_lock);
m_object = source.m_object;
m_initialized = source.m_initialized;
}
Expand All @@ -64,10 +64,15 @@ class atomic_object {
atomic_object<object, lock>& operator = (const atomic_object<object>& source) {
if (this == &source)
return *this;
std::lock_guard<lock> lock_this(m_lock);
std::lock_guard<lock> locksource(source.m_lock);
//IMPORTANT: Aquisition of both locks must be atomic.
//The following code:
// m_initialized = source.initialized(m_object);
//could deadlock with its counterpart in source.
std::lock(m_lock, source.m_lock);
m_object = source.m_object;
m_initialized = source.m_initialized;
m_lock.unlock();
source.m_lock.unlock();
return *this;
}

Expand Down Expand Up @@ -96,7 +101,7 @@ class atomic_object {
}

///<summary>
///Atomic copy of this location to argument location, only if this has location.
///Atomic copy of this object to target, only if initialized() == true.
///</summary>
///<return>True if the object was not assigned default values</return>
bool initialized(object& target) {
Expand Down

0 comments on commit 2014716

Please sign in to comment.