pthread_spin_lock无效

时间:2016-11-03 10:09:45

标签: c++ c pthreads

我有以下代码:

// temp.h

#include <iostream>
#include <pthread.h>

static pthread_spinlock_t mylock;

__attribute__((constructor))
void init_lock() {
   if(pthread_spin_init(&my_lock,0) != 0) {
      // hard exit !!
      exit(1);
   }
}

__attribute__((destructor))
void uninit_lock() {
   if(pthread_spin_destroy(&my_lock) != 0) {
      // hard exit !!
      exit(1);
   }
}

#define EXPAND_VAR(ARG1, ARG2) \
   do {\
      assert(pthread_spin_lock(&my_lock) == 0);
       /// do something
      assert(pthread_spin_unlock(&my_lock) == 0);\
    }while(0)

// main.cpp

#include "temp.h"
#define NUM_THREADS 5

void *logs(void *i){
   int j;
   for(j = 0 ; j < 10000 ; j++){
      EXPAND_VAR(j, j+1);
   }
    std::cout << "returning from thread func" << std::endl;
    pthread_exit(NULL);
}


int main(int argc, char* argv[]) {
   int i;
   pthread_t threads[NUM_THREADS];

   for( i = 0; i < NUM_THREADS; i++) {
        std::cout << " creating threads" << std::endl;
        rc = pthread_create(&threads[i],NULL,logs,(void *)i);
   }

   std::cout << "returning from main" << std::endl;
   pthread_exit(NULL);
   return 0;
}

执行上述代码时,'pthread_spin_lock'总是被阻止。我尝试了'pthread_spin_trylock'并且传递了返回值0。

我错过了'pthread_spin_lock'无效的任何内容?

我正在使用gcc 4.1.2版:(

0 个答案:

没有答案