void barber_action() { //funkcja bedaca procesem fryzjera while(1) { //this_thread::sleep_for(30ms); // cout<<"t\n"; int cust; customer_ready.wait(); queue_lock.wait(); cust=waiting.front(); waiting.pop(); queue_lock.signal(); barber_ready.signal(); //cout<<"lol"<<endl; do_the_service(cust); } }
unsigned long WINAPI barberFunc(void * data) { while (shopOpen) { customersWaiting.wait(); seatsMutex.wait(); numberOfFreeSeats++; barberReady.signal(); seatsMutex.signal(); std::cout << "Barber\tI am cutting someones hair" << std::endl; Sleep(20); } return 0; }
void notifier() { //time_t begin=time(NULL); while(1) { // cout<<"aa"<<endl; notify_lock.wait(); while(!notifies.empty()) { pair<int,int> i=notifies.front(); if(i.first==QUEUE_FULL) cout<<"Kolejka pelna. Nie umieszczono klienta nr "<<i.second<<endl; else if(i.first==CUSTOMER_PUT_IN_QUEUE) cout<<"Umieszczono w kolejce klienta nr "<<i.second<<endl; else // CUSTOMER_SERVICED cout<<"Obsluzono klienta nr "<<i.second<<endl; notifies.pop(); } //clear(notifies); notify_lock.signal(); this_thread::sleep_for(30ms); } }
unsigned long WINAPI customerFunc(void * data) { seatsMutex.wait(); if (numberOfFreeSeats > 0) { numberOfFreeSeats--; std::cout << ((int) data) << "\tI have taken a seat." << std::endl; customersWaiting.signal(); seatsMutex.signal(); barberReady.wait(); std::cout << ((int)data) << "\tI am having my hair cut. Yay." << std::endl; } else { seatsMutex.signal(); std::cout << ((int)data) << "\tShop full. Not getting hair cut." << std::endl; } return 0; }
void function(void){ for(int i = 0; i < 5; i++){ counter_mutex.wait(); for(int j = 0; j < 1000; j++){ result = result + sin(counter) * tan(counter); } counter++; counter_mutex.signal(); } }
void do_the_service(int customer) { //funkcja obslugujaca klienta //tutaj czekamy losowy kwant czasu srand(time(NULL)); int timer=(rand()%3)+1; time_t begin_time=time(NULL); while(time(NULL)-begin_time<timer); notify_lock.wait(); notifies.push(make_pair(CUSTOMER_SERVICED,customer)); notify_lock.signal(); }
pair<int,int> notify_pop() { //funkcja pobierajaca komunikat ze stosu komunikatow //jak nie ma komunikatu, zwracana jest wartosc specjalna pair <int,int> element; notify_lock.wait(); if(notifies.empty()) element=make_pair(NO_NOTIFIES,0); else { element=notifies.front(); notifies.pop(); } notify_lock.signal(); return element; }
void customer_action() { //funkcja bedaca procesem klienta time_t begin=time(NULL); while(1) { queue_lock.wait(); // cout<<waiting.size()<<endl; if( customer_is_to_be_produced()) if(waiting.size()<=MAX_QUEUE) { // cout<<"ff"<<endl; int new_cust=produce_customer(); waiting.push(new_cust); notify_lock.wait(); notifies.push(make_pair(CUSTOMER_PUT_IN_QUEUE, new_cust)); notify_lock.signal(); customer_ready.signal(); queue_lock.signal(); barber_ready.wait(); // cout<<"ff"<<endl; } else { notify_lock.wait(); notifies.push(make_pair(QUEUE_FULL, produce_customer())); notify_lock.signal(); queue_lock.signal(); } else queue_lock.signal(); // this_thread::sleep_for(30ms); } }