@ -38,7 +38,7 @@ void start_all_threads_SharedData(THIS(SharedData)){
else {
residents = this - > main_building - > residents ;
visitors = this - > main_building - > visitors ;
this - > threads_nb = residents - > get_size ( residents ) + visitors - > get_size ( visitors ) + ELEVATOR_NB ;
this - > threads_nb = residents - > get_size ( residents ) + visitors - > get_size ( visitors ) + ELEVATOR_NB + 1 ;
this - > threads = malloc_or_die ( sizeof ( pthread_t * ) * this - > threads_nb ) ;
for ( i = 0 ; i < this - > threads_nb ; i + + )
@ -49,35 +49,37 @@ void start_all_threads_SharedData(THIS(SharedData)){
/* starts threading elevators */
for ( i = 1 ; i < ELEVATOR_NB ; i + + ) {
for ( i = 0 ; i < ELEVATOR_NB ; i + + ) {
this - > start_thread ( this , this - > main_building - > elevators [ i ] - > runnable ,
this - > main_building - > elevators [ i ] , i ) ;
this - > main_building - > elevators [ i ] - > set_thread_number ( this - > main_building - > elevators [ i ] , i ) ;
}
/* starts threading visitors */
current = visitors - > get_head ( visitors ) ;
/* starting the elevator breaker */
this - > start_thread ( this , this - > elevator_breaker - > runnable , this - > elevator_breaker , i ) ;
this - > elevator_breaker - > set_thread_number ( this - > elevator_breaker , i ) ;
i + + ;
/* starts threading residents */
current = residents - > get_head ( residents ) ;
while ( current ! = NULL ) {
this - > start_thread ( this , ( ( Visitor * ) current - > get_data ( current ) ) - > runnable , current - > get_data ( current ) , i ) ;
( ( Visitor * ) current - > get_data ( current ) ) - > set_thread_number ( ( Visitor * ) current - > get_data ( current ) , i ) ;
this - > start_thread ( this , ( ( Resident * ) current - > get_data ( current ) ) - > runnable , current - > get_data ( current ) , i ) ;
( ( Resident * ) current - > get_data ( current ) ) - > set_thread_number ( ( Resident * ) current - > get_data ( current ) , i ) ;
current = current - > get_next ( current ) ;
this - > active_passengers + + ;
i + + ;
}
/* starts threading resident s */
current = resident s- > get_head ( resident s) ;
/* starts threading visitor s */
current = visitor s- > get_head ( visitor s) ;
while ( current ! = NULL ) {
this - > start_thread ( this , ( ( Resident * ) current - > get_data ( current ) ) - > runnable , current - > get_data ( current ) , i ) ;
( ( Resident * ) current - > get_data ( current ) ) - > set_thread_number ( ( Resident * ) current - > get_data ( current ) , i ) ;
this - > start_thread ( this , ( ( Visitor * ) current - > get_data ( current ) ) - > runnable , current - > get_data ( current ) , i ) ;
( ( Visitor * ) current - > get_data ( current ) ) - > set_thread_number ( ( Visitor * ) current - > get_data ( current ) , i ) ;
current = current - > get_next ( current ) ;
this - > active_passengers + + ;
i + + ;
}
/* starting the elevator breaker */
this - > start_thread ( this , this - > elevator_breaker - > runnable , this - > elevator_breaker , 0 ) ;
this - > elevator_breaker - > set_thread_number ( this - > elevator_breaker , 0 ) ;
pthread_mutex_unlock ( & this - > mutex_threads ) ;
pthread_mutex_unlock ( & this - > mutex_active_passengers ) ;