Is there a static initializer for pthread spin lock? I looked at pthread.h, and it doesn't seem like there is one.
I am looking for something similar to PTHREAD_MUTEX_INITIALIZER.
You can use constructor and destructor (available in gcc and clang)
#include <pthread.h>
#include <stdlib.h>
static pthread_spinlock_t lock;
__attribute__((constructor))
void lock_constructor () {
if ( pthread_spin_init ( &lock, 0 ) != 0 ) {
exit ( 1 );
}
}
int main () {
if (
pthread_spin_lock ( &lock ) != 0 ||
pthread_spin_unlock ( &lock ) != 0
) {
return 2;
}
return 0;
}
__attribute__((destructor))
void lock_destructor () {
if ( pthread_spin_destroy ( &lock ) != 0 ) {
exit ( 3 );
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With