所有线程都可以使用互斥锁

时间:2013-01-03 21:28:24

标签: pthreads mutex

我的问题是我有这段代码:

#include<stdio.h>
#include<semaphore.h>
#include<pthread.h>
    int number;
   pthread_mutex_t mutex[number];
   pthread_t threads[number];


void *dosomething(void *num)
{
   int *i=num;
   pthread_mutex_lock(&mutex[i]);
    //dosomething
   pthread_mutex_unlock(&mutex[i]);
}


int main(int argc, char *argv[]) //<-- main
{

    printf("How many threads do you want?");
     scanf("%d",&number);

     int rc,t;
   for(t=1;t<number;t++){
     pthread_mutex_init(&mutex[t],NULL);
     printf("In main: creating thread %d \n", t);
     rc = pthread_create(&threads[t], NULL, philospher,(void *)t);
            }

     if (rc){
       printf("ERROR; return code from pthread_create() is %d\n",rc);

      exit(0);
            }


   pthread_exit(NULL);
}

当我尝试编译它时,它说:       在文件范围内修改'mutex'/       在文件范围内修改了'threads'。 我想要它创建一些用户将定义的互斥锁和线程,并且可以从创建的所有线程中获得。

0 个答案:

没有答案