Click here to Skip to main content
13,736,490 members
Rate this:
 
Please Sign up or sign in to 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 13-Oct-18 11:54am
Updated 13-Oct-18 12:07pm

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Try
pthread_create( &pid[m], NULL, f0, 0 );
When passing a pointer to a function the arguments are not included.
  Permalink  
Comments
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.
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 );

}
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 5 days ago
   
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web04-2016 | 2.8.180920.1 | Last Updated 13 Oct 2018
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100