'互斥'未声明(首次使用此功能) [英] 'Mutex' undeclared (first use in this function)

查看:139
本文介绍了'互斥'未声明(首次使用此功能)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个程序,它接收一个数字N作为参数,并启动N个线程,每个线程同时显示1到N之间的一个数字,以产生有序序列(12 ... N)*。其中,必须使用由所有线程执行的同一个函数,并在启动线程时传递要显示为参数的数字。这是我的代码:



I want to create a program that receives a number N as an argument and starts N threads each displaying one of the numbers from 1 to N synchronously, to produce an ordered sequence (12 ... N) *. Where, it is necessary to use one and the same function executed by all threads and pass the number to be displayed as an argument when starting the thread. Here is my code :

#include<stdlib.h>
#include<pthread.h>
#include<sys/types.h>
#include<unistd.h>
//#include <pthread.h>   // pour les threads
#include <semaphore.h> // pour les semaphores
#include <fcntl.h>     // pour les flags O_CREAT, O_EXCL, ...


// Discussion : perte de cycles
// $ gcc -Wall synchro-valeur.c -lpthread
int N;
//sem_t mutex[10];
void* f0(int j) {

    for(int i = 0; i < 100; i++) {

        if(j < N-1){

            //printf("sdfsfsfsdsds: %d\n",j);

            sem_wait(&mutex[j]);
            //puts("lol");
            char k = (char)(j+1);
            puts(k+"\n");
            sem_post(&mutex[j+1]);
        }
        else{
            sem_wait(& mutex[N-1]);
            char k = (char)N;
            puts(k+"\n");
            sem_post(&mutex[0]);
        }



    }
    return NULL;
}



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

    N = atoi(argv[1]);

    sem_t mutex[N];



    unsigned int value[N];

    /*value[0]=1;

    for(int i =1;i<N;i++){
        value[i] = 0;
    }
    */

    sem_init(&mutex[0],0,1);
    for(int i =1;i<N;i++){
        sem_init(&mutex[i],0,0);
    }
    pthread_t pid[N];

    for (int j=0;j<N;j++){

        pthread_create(&pid[j],NULL,f0(j),NULL);

    }
    //puts("heyG");
    for (int i=0;i<N;i++){
        pthread_join(pid[i],0);
    }
    //puts("heyH");
    for (int i=0;i<N;i++){
        sem_destroy(&mutex[i]);
    }
    //puts("heyN");
    return EXIT_SUCCESS;
    //return 0;
}





我不知道如何解决这个错误

错误:'mutex'unclaclared(首次在此函数中使用)sem_wait (& mutex [j]);



我尝试了什么:



PLease,任何人都可以帮我修复这个错误

谢谢你



I don't know how to resolve this error
error: ‘mutex’ undeclared (first use in this function) sem_wait(&mutex[j]);

What I have tried:

PLease, can anyone help me to fix this error
THank you

推荐答案

gcc -Wall synchro-valeur .c -lpthread
int N;
// sem_t mutex [10];
void * f0( int j){

for int i = 0 ; i < 100 ; i ++){

if (j < N-1){

// printf(sdfsfsfsdsds:%d \ n ,j);

sem_wait(& mutex [j]);
// puts(lol);
char k =( char )(j + 1);
puts(k + \ n);
sem_post(& mutex [j + 1]);
}
else {
sem_wait(& mutex [N-1]);
char k =( char )N;
puts(k + \ n);
sem_post(& mutex [ 0 ]);
}



}
return NULL;
}



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

N = atoi(argv [ 1 ]);

sem_t mutex [N];



unsigned int value [N] ;

/ * value [0] = 1;

for(int i = 1; i< N; i ++){
value [i] = 0;
}
* /


sem_init(& mutex [ 0 ], 0 1 );
for int i = 1 ; i< N; i ++){
sem_init(& mutex [i], 0 0 );
}
pthread_t pid [N];

for int j = 0 ; j
pthread_create(& pid [j],NULL,f0(j),NULL);

}
// puts(heyG);
for int i = 0 ; i< N; i ++){
pthread_join(pid [i], 0 );
}
// puts(heyH);
for int i = 0 ; i< ; N; i ++){
sem_destroy(& mutex [i]);
}
// puts(heyN);
return EXIT_SUCCESS;
// 返回0;
}
gcc -Wall synchro-valeur.c -lpthread int N; //sem_t mutex[10]; void* f0(int j) { for(int i = 0; i < 100; i++) { if(j < N-1){ //printf("sdfsfsfsdsds: %d\n",j); sem_wait(&mutex[j]); //puts("lol"); char k = (char)(j+1); puts(k+"\n"); sem_post(&mutex[j+1]); } else{ sem_wait(& mutex[N-1]); char k = (char)N; puts(k+"\n"); sem_post(&mutex[0]); } } return NULL; } int main(int argc, char *argv[]) { N = atoi(argv[1]); sem_t mutex[N]; unsigned int value[N]; /*value[0]=1; for(int i =1;i<N;i++){ value[i] = 0; } */ sem_init(&mutex[0],0,1); for(int i =1;i<N;i++){ sem_init(&mutex[i],0,0); } pthread_t pid[N]; for (int j=0;j<N;j++){ pthread_create(&pid[j],NULL,f0(j),NULL); } //puts("heyG"); for (int i=0;i<N;i++){ pthread_join(pid[i],0); } //puts("heyH"); for (int i=0;i<N;i++){ sem_destroy(&mutex[i]); } //puts("heyN"); return EXIT_SUCCESS; //return 0; }





我不知道如何解决这个错误

错误:'mutex'unclaclared(首次在此函数中使用)sem_wait (& mutex [j]);



我尝试了什么:



PLease,任何人都可以帮我解决这个错误

谢谢你



I don't know how to resolve this error
error: ‘mutex’ undeclared (first use in this function) sem_wait(&mutex[j]);

What I have tried:

PLease, can anyone help me to fix this error
THank you


mutex 被声明为 main 函数的一部分:

mutex is declared as part of your main function:
int main(int argc, char *argv[]) {

    N = atoi(argv[1]);

    sem_t mutex[N];

这使得它成为一个局部变量 - 它的范围仅限于最近包含它的大括号集合 - main 函数本身。

然后你尝试在 f0 函数中使用它:

which makes it a local variable - its scope is limited to the set of curly brackets which most recently enclose it - the main function itself.
And then you try to use it within the f0 function:

void* f0(int j) {

    for(int i = 0; i < 100; i++) {

        if(j < N-1){

            //printf("sdfsfsfsdsds: %d\n",j);

            sem_wait(&mutex[j]);

因为 f0 函数中不存在全局或局部变量,系统正确地抱怨。



可能,你想让 mutex 全局(通过在体外声明它)任何函数),或将其作为参数传递给 f0 函数以及 j

Because no global or local variable exists in the f0 function, the system rightly complains.

Possibly, you want to make mutex either global (by declaring it outside the body of any function), or pass it as a parameter to the f0 function along with j


第一个错误就在这一行真的很奇怪。编译器应该已经抱怨:

Really strange that the first error is at this line. The compiler should complain already on:
sem_t mutex[N];

这段代码完成了工作

This code shuld do the job

sem_t *mutex = malloc( sizeof( sem_t ) * N );
//at the ende
free( mutex );

提示:将警告级别设置得更高。这个警告可以帮助你编写好的代码: - )

Tip: set the warning level higher. This warnings are helping you to write good code :-)


这篇关于'互斥'未声明(首次使用此功能)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆