Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
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 :

C#
#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;
}


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
Posted
Updated 11-Oct-18 2:36am

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

    N = atoi(argv[1]);

    sem_t mutex[N];
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]);
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
 
Share this answer
 
Comments
Member 14016044 11-Oct-18 8:30am    
This code gives me the error I posted above
OriginalGriff 11-Oct-18 8:32am    
Yes. And it will continue to do so until you read what I said, and fix it...
Really strange that the first error is at this line. The compiler should complain already on:
C++
sem_t mutex[N];
This code shuld do the job
C++
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 :-)
 
Share this answer
 
v2
Comments
Member 14016044 11-Oct-18 8:43am    
I did what you say
but I had this error

error: initializer element is not constant
sem_t *mutex = malloc( sizeof( sem_t ) * N );
KarstenK 12-Oct-18 13:23pm    
I think you must write sem_t *mutex; at start of the function, because C needs all vars at the begin.
If the code you have posted as a solution is genuinely the code causing the error then try changing
C++
sem_wait(& mutex[N-1]);
to
C++
sem_wait(&mutex[N-1]);
 
Share this answer
 
Comments
Member 14016044 11-Oct-18 8:39am    
I still have the error
CHill60 11-Oct-18 9:24am    
And the error is still "‘mutex’ undeclared (first use in this function) sem_wait(&mutex[j]);"?
Have you actually tried @OriginalGriff's suggestions?
Member 14016042 11-Oct-18 10:24am    
oh I did not see that ! Thank you
Sorry I copied the wrong code.
#include<stdio.h>
#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[N];
void* f0(int j) {
	//sem_t mutex[N];
    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;
}
 
Share this answer
 
Comments
CHill60 11-Oct-18 8:34am    
If you need to improve your question use the "Improve Question" link next to it. If it is not visible hover your mouse over your original post and it should appear.
Richard Deeming 12-Oct-18 13:33pm    
If this was posted by the OP, then he's created a sock-puppet account to post it - they're two different accounts.
Richard MacCutchan 12-Oct-18 13:36pm    
Average QA poster - not quite as bright as the Toc-H lamp.
CHill60 12-Oct-18 13:41pm    
User names weren't showing when I looked at this, only just realised. One to keep an eye on!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900