Kończenie wątku +mutexy

0

Witam,
robię zadanie operując na problemie ucztujących filozofów(http://pl.wikipedia.org/wiki/Problem_ucztuj%C4%85cych_filozof%C3%B3w), operując na wygooglanym kodzie http://www.daniweb.com/software-development/c/threads/274898 chciałem przerobić trochę program, aby wyłapać zakończenie wątku(Filozof zje posiłek 5 razy) funkcją "pthread_join" jednak dla kodu:

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#include <stdbool.h>
#include <errno.h>

#define NUM_PHILOSOPHERS 5;

typedef struct philos {
    char *status;
    pthread_mutex_t *right, *left;
} Philosopher;

void philosopherSetup(Philosopher *philosopher, pthread_mutex_t *left, pthread_mutex_t *right) {
    philosopher->left = left;
    philosopher->right = right;
}

void *philosopherRun(void *phil) {
    Philosopher *philosopher = phil;
    int eat =0;
    while (eat < 5) {
        philosopher->status = "thinking";
        philosopher->status = "waiting";
        bool both = false;
        do {
            if (pthread_mutex_lock(philosopher->left) != 0)
                fprintf(stderr, "Left lock error.\n");
            int right_lock_result = pthread_mutex_trylock(philosopher->right);
            switch (right_lock_result) {
                case 0:
                    both = true;
                    eat++;
                    break;
                case EBUSY:
                    pthread_mutex_unlock(philosopher->left); // try again
                    both = false;
                    break;
                default:
                    fprintf(stderr, "Right lock error\n.");
            }
        } while (!both);
        printf("."); // this won't appear if blocked
        philosopher->status = "eating";
        sleep(1);
        pthread_mutex_unlock(philosopher->left);
        pthread_mutex_unlock(philosopher->right);
    }
    int status = 44;
    return (void *)status;
}

/*
 * Starts all of the philosopher threads and reports on their progress.
 */
int main(int argc, char** argv) {
    int number = NUM_PHILOSOPHERS;
    if (argc > 1) {
        number = atoi(argv[1]);
    }
    printf("Creating %d philosophers.\n", number);
    Philosopher philosophers[number];
    pthread_mutex_t forks[number];
    pthread_t threads[number];
    
    int i;
    for (i = 0; i < number; i++) { // create the locks
        if (pthread_mutex_init(&forks[i], NULL)) {
            fprintf(stderr, "Unable to create locks.\n");
            return (EXIT_FAILURE);
        }
    }
    for (i = 0; i < number; i++) { // setup the philosophers
        philosopherSetup(&philosophers[i], &forks[i], &forks[(i + 1) % number]);
    }
    
    for (i = 0; i < number; i++) { // create and start the threads
        pthread_create(&threads[number], NULL, philosopherRun, &philosophers[i]);
    }
    //komuinakty o zakonczeniu watkow
    for (i = 0; i < number; i++) { // create the locks
        int r = pthread_join(threads[i], NULL);
        printf("Watek %d, zakonczyl sie statusem %d\n", i, r);
    }
    
    return (EXIT_SUCCESS);
}

dostaje zwrotkę:

 ..Watek 0, zakonczyl sie statusem 3
Watek 1, zakonczyl sie statusem 3
Watek 2, zakonczyl sie statusem 3
Watek 3, zakonczyl sie statusem 3
Watek 4, zakonczyl sie statusem 3

Czyli kod błędu nr 3.
Wydaje mi się, że chodzi o to, że wątek niby się kończy ale dalej ma aktywne mutexy i pthread_join nie zwraca odpowiedniej wartości - zera. Tylko nie wiem jak to naprawić aby móc odebrać poprawny komunikat o zakończeniu wątku.
Możecie mi jakoś pomóc?

==EDIT
albo jeszcze inny pomysł na możliwy błąd - po zakończeniu wątku wartość right w strukturze poprzedniego filozofa powinna być ustawiona na tego z right kończącego się wątku. Ale nie jestem pewien czy o to chodzi ani tym bardziej czy zrozumiale opowiadam o problemie ;)

0

ehh... problem filozofow ile to ja sie nasiedzialem nad tym zadaniem ;p Niestety my musielismy to pisac pod linuxem... w bashu. Jesli Ci sie przyda moge poszukac gdzies kod, moze jeszcze go mam gdzies.

Zadanie nie nalezy do najlatwiejszych trzeba uwazac na problem zaglodzenia i rownomiernie rodzielac widelce pomiedzy filozofow.

1 użytkowników online, w tym zalogowanych: 0, gości: 1