// -*- c++ -*-
#ifndef LOCKS_H
#define LOCKS_H

#include <cstdio>
#include <pthread.h>

namespace locks {

template <typename L>
void unlocker(void* lock_ptr)
{
    L* lock = (L*) lock_ptr;
    fprintf(stderr, "Releasing lock (unchecked) during cancellation!\n");
    lock->unlock();
}

template <typename L>
void unlocker_checked(void* lock_ptr)
{
    L& lock = *((L*) lock_ptr);
    if (lock.owns_lock()) {
        fprintf(stderr, "Releasing lock (checked) during cancellation!\n");
        lock.unlock();
    }
}

}

#endif /* LOCKS_H */

// -*- c++ -*-
#ifndef LOCKS_H
#define LOCKS_H

#include <pthread.h>

namespace locks {

template <typename L>
void unlocker(void* lock_ptr)
{
    L* lock = (L*) lock_ptr;
    lock->unlock();
}

template <typename L>
void unlocker_checked(void* lock_ptr)
{
    L& lock = *((L*) lock_ptr);
    if (lock)
        lock.unlock();
}

}

#endif /* LOCKS_H */

--- 
+++ 
@@ -2,6 +2,7 @@
 #ifndef LOCKS_H
 #define LOCKS_H
 
+#include <cstdio>
 #include <pthread.h>
 
 namespace locks {
@@ -10,6 +11,7 @@
 void unlocker(void* lock_ptr)
 {
     L* lock = (L*) lock_ptr;
+    fprintf(stderr, "Releasing lock (unchecked) during cancellation!\n");
     lock->unlock();
 }
 
@@ -17,8 +19,10 @@
 void unlocker_checked(void* lock_ptr)
 {
     L& lock = *((L*) lock_ptr);
-    if (lock)
+    if (lock.owns_lock()) {
+        fprintf(stderr, "Releasing lock (checked) during cancellation!\n");
         lock.unlock();
+    }
 }
 
 }
Add logging etc. to lock release code.
