我的问题是我有这段代码:
#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);
}
当我尝试编译它时,它说:在文件范围内可变地修改了“互斥锁”/在文件范围内可变地修改了“线程”。我只希望它创建许多用户将定义的互斥锁和线程,并且可以从所有创建的线程中使用。