My function have to stop a thread only if n threads are running including itself [closed]
Thread T12 is only allowed to terminate while 6 threads (with also with himself) runs. "args" is the argument of the function that gives me the id of the running thread .
My code is running, but it exceeds its time limit.
THREAD_struct * t1 = (THREAD_struct*) args ;
if(t1->id != 12){
pthread_mutex_lock(&lock) ;
while(t12In == false ){ // global variable ->t12In was initialized with false
pthread_cond_wait(&cond ,&lock);
}
pthread_mutex_unlock(&lock) ;
}
pthread_mutex_lock(&lock) ;
runningTh++;
if (runningTh == simThreads ){
pthread_cond_broadcast(&cond) ;
}
while(t1->id != 12 && runningTh <= simThreads){
pthread_cond_wait(&cond ,&lock);
}
pthread_mutex_unlock(&lock) ;
pthread_mutex_lock(&lock);
if (t1->id == 12) {
t12In = true ;
pthread_cond_broadcast(&cond) ;
while (runningTh < simThreads){
pthread_cond_wait(&cond ,&lock);
}
}
pthread_mutex_unlock(&lock);
pthread_mutex_lock(&lock);
if (t1->id == 12) {
t12In = true ; runningTh++ ;
pthread_cond_broadcast(&cond) ;
}
pthread_mutex_unlock(&lock);