linux - Synchronization not working properly when Multi-threading in C -


i trying create simple bar program amount of customers can inside bar @ same time. , every time customer asks beer, bartender should server customer beer.

for reason in program, bar tender serves customer after he/she leaves bar

how can fix this? suggestions?

here code:

#include <pthread.h> #include <stdio.h> #include <stdlib.h> //for declaration of exit() #include <semaphore.h> //to use semaphores  pthread_mutex_t serve = pthread_mutex_initializer; pthread_barrier_t barrier1; sem_t oktoenter;  int cid = 0;  void enterbar(); void orderstart(); void servestart(); void servedone(); void orderdone(); void drinkbeer(); void leavebar();  void* bartender(void *arg) {     servestart();     servedone(); }  void* customer(void* id) {     cid =(int)id;     enterbar();     leavebar();  }  void enterbar(){     printf("customer %d enters bar.\n", cid);     int cups;     pthread_t order;      for(cups=0;cups<(cid%3+1);cups++){         pthread_mutex_lock(&serve);              orderstart();         orderdone();         drinkbeer();                 pthread_mutex_unlock(&serve);     }     //decrease semaphore } void orderstart(){     pthread_t order;     printf("customer %d asks beer.\n", cid);     int rc = pthread_create(&order, null, bartender, null);  } void orderdone(){     printf("customer %d gets beer.\n", cid);  } void drinkbeer(){     printf("customer %d drinks beer.\n", cid); } void leavebar(){     printf("customer %d leaves bar.\n", cid);     //increase semaphore }  void servestart(){     printf("bartender starts serve customer %d.\n", cid); }  void servedone(){     printf("bartender done serving customer %d.\n", cid); }  int main (int argc, char *argv[]) {     int t;     long rc;         int num_customers = atoi(argv[1]); //number of customers     int capacity = atoi(argv[2]); //bar capacity      if(num_customers > 0 && capacity > 0){         pthread_t threads[num_customers];         if(random() > rand_max / 2)             usleep(1);         //rc = sem_init(&sem1,0,capacity);         rc = pthread_barrier_init(&barrier1, null, num_customers);         for(t=0; t< num_customers; t++){                 printf("in main: creating thread %d\n", t);                 rc = pthread_create(&threads[t], null, customer, (void* )t);                 if (rc){                     printf("error; return code pthread_create() %ld\n", rc);                     exit(-1);                 }         }     }     else{             printf("error: both parameters should valid positive numbers.");             exit(-1);     }      /* last thing main() should */     pthread_exit(null); } 

i worked around changing functions not work properly.

first, great little project in understanding threads , how life multi-threaded.

there problems above scenarios. first off, neither of keeping track of how many customers in bar enterbar() , leavebar() functions. in these functions, need keep track of current bar count. once bar full, must lock door no other customers can enter. when customer leaves bar, must unlock door @ least 1 more customer can enter. also, given complete problem scenario, should create 1 bartender (i.e. 1 bartender thread) , syncronize him using pthread_cond_wait/pthread_cond_signal pour beer once customer asks it. can't keep killing off , recreating bartender each drink poured, , remember 1 customer can ask beer @ time. should done pthread_mutex_lock/unlock's.


Comments

Popular posts from this blog

c# - SVN Error : "svnadmin: E205000: Too many arguments" -

c++ - Using OpenSSL in a multi-threaded application -

All overlapping substrings matching a java regex -