Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
hello!!!
I want to Create a program that receives a number N as an argument and starts N threads each displaying one of a number from 1 to N synchronously (with named or anonymous semaphores of choice) to produce an ordered sequence (12 ... N) *.
here is my code

int N;
void* f0(int k, sem_t *mutex) {
	
    for(int i = 0; i < 100; i++) {
    	if(k < N-1){
    		sem_wait(&mutex[k]);
    		//printf("value[] = %d", value[k]);
    		printf("%d\n",k+1);
    		k = k + 1;
    		sem_post(&mutex[k]);
    		//printf("i =%d \n",i);
    		

    	}
    	
    	else{
    		
    		sem_wait(&mutex[N-1]);
    		printf("%d\n",k+1);
    		k = N-k-1;
    		sem_post(&mutex[k]);
    		//printf("i =%d \n",i);
    		

    	}
    }
    
    return NULL;
}
int main(int argc, char *argv[]) {
	N = atoi(argv[1]);
	int *value;
	value= malloc(sizeof(int)*N);
	sem_t *mutex;
	mutex =malloc(sizeof(sem_t)*N);
	pthread_t pid[N];
	
	for (int i =0;i<N;i++){
		if(i==0){
			value[i]=1;
		}
		else{
			value[i]=0;
		}
	}
	for(int i=0;i<N;i++){
		sem_init(&mutex[i],0,value[i]);
	}
	int m =0;
	while(m<N){
        pthread_create(&pid[m], NULL, f0(m,&mutex),0);
        m=m+1;
        printf("m = %d\n", m );
        
	}
	printf("heyG\n");
	for (int i=0;i<N;i++){
		pthread_join(pid[i],0);
	}
	for (int i=0;i<N;i++){
		sem_destroy(&mutex[i]);
	}
	return EXIT_SUCCESS;
}


What I have tried:

I get this warning and I want to fix it

copie.c: In function ‘main’:
copie.c:60:44: warning: passing argument 2 of ‘f0’ from incompatible pointer type [-Wincompatible-pointer-types]
         pthread_create(&pid[m], NULL, f0(m,&mutex),0);
                                            ^
copie.c:11:7: note: expected ‘sem_t * {aka union <anonymous> *}’ but argument is of type ‘sem_t ** {aka union <anonymous> **}’
 void* f0(int k, sem_t *mutex) {
Posted
Updated 13-Oct-18 12:07pm

1 solution

Try
C++
pthread_create( &pid[m], NULL, f0, 0 );
When passing a pointer to a function the arguments are not included.
 
Share this answer
 
Comments
Member 14016042 13-Oct-18 18:21pm    
copie.c: In function ‘main’:
copie.c:61:39: warning: passing argument 3 of ‘pthread_create’ from incompatible pointer type [-Wincompatible-pointer-types]
pthread_create(&pid[m], NULL, f0,0);
^
In file included from copie.c:3:0:
/usr/include/pthread.h:233:12: note: expected ‘void * (*)(void *)’ but argument is of type ‘void * (*)(int, sem_t *) {aka void * (*)(int, union <anonymous> *)}’
extern int pthread_create (pthread_t *__restrict __newthread,
it gives me this warning
Rick York 13-Oct-18 19:04pm    
Here's the prototype for the function:
int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg );
I find typedefs to be helpful in these situations.
typedef void * pvoid;
typedef pvoid (* threadfunc)( pvoid );
Your function, f0, does not comply with that prototype so you have to modify it until it does. You can define a structure that contains the data items you want to pass if you want to. I have done that in the past and it works. For you, the members of that structure would be an integer and a pointer to a sem_t.
Member 14016042 13-Oct-18 19:20pm    
Here is what I did
struct DataItem
{
int k;
sem_t *mutex;
};

void* f0(struct DataItem data) {

for(int i = 0; i < 100; i++) {
if(data.k < N-1){
sem_wait(&(data.mutex)[data.k]);
//sem_wait(&mutex[k]);
printf("%d\n",data.k + 1);
//k = k + 1;
//sem_post(&mutex[k+1] );
sem_post(&(data.mutex)[data.k + 1]);
//printf("i =%d \n",i);


}

else{

//sem_wait(&mutex[k]);
sem_wait(&(data.mutex)[data.k]);

printf("%d\n",data.k + 1);
//k = N-k-1;
//sem_post(&mutex[N-k-1]);
sem_post(&(data.mutex)[N - data.k - 1]);
//printf("i =%d \n",i);


}
}

return NULL;
}
and in the main function :
while(m<N){
struct DataItem data;
data.k = m;
data.mutex = &mutex;
pthread_create(&pid[m], NULL, f0(data),0);
m=m+1;
printf("m = %d\n", m );

}
Member 14016042 13-Oct-18 19:21pm    
and I get this warning

copie.c:73:14: warning: assignment from incompatible pointer type [-Wincompatible-pointer-types]
data.mutex = &mutex;
Rick York 13-Oct-18 23:56pm    
If mutex is declared and used as before, I would guess that should be Hide   Copy Code
data.mutex = &mutex[m];
Also, I think it should be:
pthread_create( &pid[n], NULL, f0, &data[m] );
data is the argument in the thread creation function. You probably do not want to share one instance of data with all of the threads so make an array of the structures, set each one appropriately, and pass the address of that one to the thread.
DataItem * data = (DataItem *)calloc( sizeof( DataItem ), N );
while( m < N )
{
   data[m].k = m;
   data[m].mutex = &mutex[m];
   pthread_create( &pid[m], NULL, f0, &data[m] );
   ++m;
   printf("m = %d\n", m );
}

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