Thread: It seems do not pass auguments

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    4

    It seems do not pass auguments

    It is a dining philosopher problem.
    I used mutual exclusion. No compile error, but it does not work.
    I try to print "test" in the route "philosopher",howerver....
    Could you help me check what is wrong?

    Code:
    #include <stdio.h>
    #include <pthread.h>
    #include <malloc.h>
    #include <stdlib.h>
    #include <time.h>
    #define N 5
    
    struct da {
        int id;
    };
    struct da myda[N];
    
      pthread_mutex_t  *mymutex;
    
    void think (int i) {
    	printf ("\nPhilosopher no. %d is thinking...\n", i);
                    sleep(2);
    }
    
    void eat (int i) {
    	printf ("\nPhilosopher no. %d is eating...\n", i);
    	sleep(1);
    }
    
    void *philosopher (void *arg)
    {
          int i;
          struct da *mydata;
          mydata = (struct da *) arg; 
          i = mydata->id;
              printf("test");//does not execute!
                  printf("\nPhilosopher %d\n",i);
                  think(i);
                  if (i%2 == 1) {
                  pthread_mutex_lock(&mymutex[i+1]);    
                  pthread_mutex_lock(&mymutex[i]);
                  eat(i);
                  pthread_mutex_unlock(&mymutex[i]);   
                  pthread_mutex_unlock(&mymutex[i+1]); 
               }
                  else
                  { 
                       if  (i == 4) {
                       pthread_mutex_lock(&mymutex[i]);
                       pthread_mutex_lock(&mymutex[0]);
                       eat(i);
                       pthread_mutex_unlock(&mymutex[0]);
                       pthread_mutex_unlock(&mymutex[i]);
                       }              
                     {
                  pthread_mutex_lock(&mymutex[i]);     
                  pthread_mutex_lock(&mymutex[i+1]); 
                  eat(i);
                  pthread_mutex_unlock(&mymutex[i+1]);  
                  pthread_mutex_unlock(&mymutex[i]); 
                     }
                  }
                  pthread_exit(NULL);
    }
       
    void main (int argc, char*argv[]) {
            pthread_t threads[N]; 
            int rc, i;
            
            int *id[N];
            
            /*pthread_t threads[N];*/
            mymutex = (pthread_mutex_t*)malloc(sizeof(mymutex) * N);
            printf("\nThere are %d philosophers\n", N);
           
            for (i = 0; i<N; i++) {  
                 	pthread_mutex_init(&mymutex[i], NULL);   
                 }    
            for (i = 0; i<N; i++) {
            myda[i].id = i;
      rc = pthread_create(&threads[i], NULL, philosopher,(void *)&myda[i]);
            if (rc) {
               printf("ERROR;return code from pthread_create() is %d\n",rc);
               exit(-1);
              }
    	}
            for (i = 0; i<N; i++) {
               pthread_mutex_destroy(&mymutex[i]);
            } 
    }

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    I've never heard of pthread.h so I can't compile. I don't think it's standard.

    Just by looking at it, I couldn't tell you why that wouldn't print. Are you sure it's entering the function? Does it print the next output line right below it?
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    I've never heard of pthread.h so I can't compile. I don't think it's standard.
    It's the POSIX threading library.

    The code isnt executing because your main() thread is ending. Try calling pthread_join() on the created thread.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Speed test result
    By audinue in forum C Programming
    Replies: 4
    Last Post: 07-07-2008, 05:18 AM
  2. Replies: 3
    Last Post: 11-22-2007, 12:58 AM
  3. Pass by reference
    By mcgeady in forum C Programming
    Replies: 11
    Last Post: 02-17-2005, 03:01 AM
  4. pass be reference versus pass by value
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 08-01-2002, 01:03 PM
  5. Replies: 3
    Last Post: 04-02-2002, 01:39 PM