HP aC++/HP C A.06.20 Programmer's Guide
+check=lock
The +check=lock option enables checking of C and C++ user applications that use
pthreads. The option reports violations of locking discipline when appropriate locks
are not held while accessing shared data by different threads. The check is based on
the lockset method for detecting locking discipline violations, as described in the Eraser
tool article at http://citeseerx.ist.psu.edu/viewdoc/
download?doi=10.1.1.22.3256&rep=rep1&type=pdf.
Note that +check=all does not enable +check=lock. Also note that since
+check=lock requires instrumenting each memory access, it can result in a
considerable slowdown of the application at runtime. +check=lock also increases
the memory consumption of the instrumented application.
The check is performed on each memory access. It detects violations in locking discipline
for mutual exclusion locks (mutexes) for applications using posix threads. When the
locking discipline is violated, it is reported along with the line number and the address
for which the violation occurs. Consider the following code example:
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
unsigned long things_done=0;
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t mutex2 = PTHREAD_MUTEX_INITIALIZER;
void *thread1(void *arg) {
pthread_mutex_lock(&mutex1);
things_done++;
pthread_mutex_unlock(&mutex1);
return 0;
}
void *thread2(void *arg) {
pthread_mutex_lock(&mutex2);
things_done++;
pthread_mutex_unlock(&mutex2);
return 0;
}
int main(int argc, char *argv[])
{
pthread_t th1, th2;
pthread_create(&th1, NULL, thread1, (void*)NULL );
pthread_mutex_lock(&mutex1);
things_done++;
pthread_mutex_unlock(&mutex1);
pthread_create(&th2, NULL, thread2, (void*)NULL );
sleep(10);
return 0;
}
cc +check=lock simple_race.c -lpthread
./a.out
Runtime Error: locking discipline violation: in file simple_race.c line 16 address 40010658
(0) 0x0000000004072ca0 _rtc_raise_fault + 0x2c0 at rtc_utils.c:382 [./a.out]
(1) 0x0000000004028650 _ZN11DRD_Runtime15HandleMemAccessEybPcjS0_ + 0x590 at lock_check.C:438
[./a.out]
(2) 0x0000000004029840 _ZN11DRD_Runtime17HandleStoreAccessEyPcjS0_ + 0x60 at lock_check.C:145
[./a.out]
(3) 0x000000000401bfa0 __DRD_RegisterStoreAccess__ + 0x160 at lock_check.H:126 [./a.out]
(4) 0x0000000004018780 thread2 + 0xd0 at simple_race.c:16 [./a.out]
(5) 0x60000000c57c3c60 __pthread_bound_body + 0x170
104 Command-Line Options