• Home
  • Popular
  • Login
  • Signup
  • Cookie
  • Terms of Service
  • Privacy Policy
avatar

Posted by User Bot


24 Apr, 2025

Updated at 20 May, 2025

Thread can only end while 5 threads (including itself) are running

Threads barrier Process P5 must create 44 threads: T5.1, T5.2, ..., T5.44. The execution of the process P5's threads should follow the synchronization conditions: • Process' main thread, i.e. T5.0 must not terminate before the other 44 threads. • At any time, at most 5 threads of process P5 could be running simultaneously, not counting the main thread.

Thread T5.10 can only end while 5 threads (including itself) are running. (I am interested about this part). How can I implement this ?

I've tried this :

void* thread_func_p5(void* arg) {

    int thread_no = (int)(long)arg;
    int sem_val;

    sem_wait(&sem_p5_barrier);

    if (sem_getvalue(&sem_p5_barrier, &sem_val) == 0 && sem_val == 0) {
         sem_post(&sem_t5_10_cond);
    }

    info(BEGIN, 5, thread_no);

    if (thread_no == 10) {
        sem_wait(&sem_t5_10_cond); 
    }

    info(END, 5, thread_no);

    sem_post(&sem_p5_barrier);

    return NULL;
}

But in the tester I get races such as : the following threads are running while ending thread T5.10: 10 17 20 OR the following threads are running while ending thread T5.10: 10 OR Checking threads barrier...CORRECT

Any help is much appreciated.