Question

I am trying to solve dining philosophers problem.So I pretty much made entire code but the problem is that I can't initialize monitors(i've made pseudocode which I re-written in c++) so really I can't test the program. Can anyone help me and say what's the issue with initialization of monitor/mutex_init ?

I get error on line 18 and it goes like: error: ‘int pthread_mutex_init’ redeclared as different kind of symbol

changing it into int pthread_mutex_init(&monitor,NULL); WON'T work !

by adding int pthread_mutex_init(pthread_mutex_t *monitor, NULL);

I get error: expected identifier before ‘__null’

#include <iostream>
#include <cstdio>
#include <pthread.h>
#include <cstdlib>
#include <unistd.h>

using namespace std;


char v_filozofi[5]={'O'};   //vizualni prikaz filozofa
int stapic[5]={1};          //stapici za filozofe
int broj[5];                //shema koju sam mora sloziti da imam broj filozofa

pthread_t       d_filozofi[5]; //dretve filozofa,philosopher's thread
pthread_cond_t  red_uvjeta[5];  
pthread_mutex_t monitor;        //deklariramo monitor,tj mymutex

int pthread_mutex_init(*monitor,NULL);

void ispisi_stanje(int n){
    for(int i = 0; i < 5 ;i++)  cout<< v_filozofi[i];
    cout<<"("<< n+1 << ")" <<endl;  
}

void misliti(int n){
    cout<<"Mislim " << endl;
    sleep(4);
}

void jesti(int n){
    pthread_mutex_lock(&monitor);
        v_filozofi[n]='o';
        while(stapic[n]==0 || stapic[n+1]%5==0){//gleda ima li lijevi i desni
        //stapic na raspolaganju
            pthread_cond_wait(&red_uvjeta[n],&monitor);
        }
        stapic[n] = stapic[(n+1)%5] = 0;
        v_filozofi[n] = 'X';

        ispisi_stanje(n);

    pthread_mutex_unlock(&monitor);
    sleep(2);
    pthread_mutex_lock(&monitor);
        v_filozofi[n] = 'O';

        stapic[n] = stapic[(n+1)%5] = 1; 

        pthread_cond_signal(&red_uvjeta[(n-1)%5]);
        pthread_cond_signal(&red_uvjeta[(n+1)%5]);

        ispisi_stanje(n);
    pthread_mutex_unlock(&monitor);


}

void * filozof(void *n){
    int br_fil = *((int *)n);
    while(1){
        misliti(br_fil);
        jesti(br_fil);
    }   
return 0;
}   

//MAIN  
int main(){
for(int i=0;i<5;i++){
    broj[i] = i;
    pthread_cond_init(&red_uvjeta[i],NULL);
    }
for(int i=0;i<5;i++){
    sleep(1);
    pthread_create( &d_filozofi[i],NULL,filozof,&broj[i]);
    }

for(int i=0;i<5;i++) pthread_join(d_filozofi[i],NULL);
pthread_mutex_destroy(&monitor);

return 0;
}
Was it helpful?

Solution

remove the line

int pthread_mutex_init(*monitor,NULL);

and instead do

pthread_mutex_init(&monitor,NULL); 

at the beginning of your main function. (and check that it returns 0)

As it is you are declaring the function not calling it, and since it's already declared you get an error

OTHER TIPS

This function expects a pointer, and why did you put "int" before the call? I think you confused a prototype (which is unecessary) and an actual call to the function.

int pthread_mutex_init(*monitor,NULL);

So:

pthread_mutex_init(&monitor,NULL);

http://pubs.opengroup.org/onlinepubs/7908799/xsh/pthread_mutex_init.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top