4

pthread スピンロック用の静的初期化子はありますか? pthread.h を見たところ、存在しないようです。

PTHREAD_MUTEX_INITIALIZER に似たものを探しています。

4

2 に答える 2

5

コンストラクタとデストラクタを使用できます (gcc と 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 );
    }
}
于 2014-07-12T11:47:31.593 に答える