Search code examples
clinuxmultithreadingpthreadsposix

Thread barrier for 49 threads


I am supposed to create 49 threads in a certain process( there are multiple processes here in my problem, so let's call the process P3). I have created those threads but the issue presents itself here: at any time, at most 5 threads are allowed to run in P3 without counting the main process. Thread 13 from P3 is allowed to end only if there are a total of 5 threads that are running(Thread 13 is among those 5 threads). My question is: how do I make sure that at some point of the program's execution there will be 5 threads running and among them there will be Thread 13 so it can end it's execution. I am using C as a programming language and Linux system calls. Moreover, I am not allowed to use "sleep()" and "usleep()".

This is a function in which I count the number of threads.

` void* thread_function2(void* arg) {

TH_STRUCT* st=(TH_STRUCT*)arg;



sem_wait(&sem);

sem_wait(&sem2);
nrThreads++;
sem_post(&sem2);

printf("Number of threads running: %d\n",nrThreads);




sem_wait(&sem3);
nrThreads--;
sem_post(&sem3);

sem_post(&sem);

return 0;

} `

This part is from the main thread in which I create my threads: sem_init(&sem,0,5); sem_init(&sem2,0,1); sem_init(&sem3,0,1); sem_init(&sem4,0,1);

        for(int i=1;i<=49;i++)
        {
            params1[i].procNum=3;
            params1[i].threadNum=i;
            pthread_create(&tids1[i],NULL,thread_function2,&params1[i]);
        }

`

Beginning a thread is done with the fuction info(args) which prints the word BEGIn and the thread number.
Ending a thread is done with a function info(args) which prints the word END and the thread number.
This is an example of an output and what the threads do when they begin and when they end:

[ ] BEGIN P5 T0 pid=30059 ppid=30009 tid=-99981504  
[ ]  END  P5 T0 pid=30059 ppid=30009 tid=-99981504  
[ ] BEGIN P6 T0 pid=30060 ppid=30009 tid=-99981504  
[ ]  END  P6 T0 pid=30060 ppid=30009 tid=-99981504  
[ ] BEGIN P7 T0 pid=30061 ppid=30009 tid=-99981504  
[ ]  END  P7 T0 pid=30061 ppid=30009 tid=-99981504  
[ ] BEGIN P8 T0 pid=30062 ppid=30009 tid=-99981504  
[ ]  END  P8 T0 pid=30062 ppid=30009 tid=-99981504  
[ ]  END  P3 T0 pid=30009 ppid=30006 tid=-99981504  
[ ] BEGIN P9 T0 pid=30063 ppid=30006 tid=-99981504  
[ ] BEGIN P9 T4 pid=30063 ppid=30006 tid=-125163776  
[ ] BEGIN P9 T1 pid=30063 ppid=30006 tid=-125163776  
[ ]  END  P9 T1 pid=30063 ppid=30006 tid=-125163776  
[ ] BEGIN P9 T2 pid=30063 ppid=30006 tid=-108378368  
[ ]  END  P9 T4 pid=30063 ppid=30006 tid=-125163776  
[ ]  END  P9 T2 pid=30063 ppid=30006 tid=-108378368  
[ ] BEGIN P9 T3 pid=30063 ppid=30006 tid=-116771072  
[ ]  END  P9 T3 pid=30063 ppid=30006 tid=-116771072  
[ ]  END  P9 T0 pid=30063 ppid=30006 tid=-99981504  
[ ]  END  P1 T0 pid=30006 ppid=3467 tid=-99981504  

Solution

  • Well,depends on if it matters when T13 will end.You can never know how long a thread will execute,so in the end there might not ever be 5 concurent threads running,If it doesnt matter when it will end you can count the number of threads and when the counter reaches 44, use a mutex on the last 5 threads.

    int threads_running=0
    pthread_t thread[49];
    pthread_mutex_t lock;
    int counter=0;
    void *thread(some_args)
    {
      if (threads_running==13) while (threads_running <5);
    
      if(counter==44)
      { 
        pthread_mutex_lock(&lock);
        //do smth
        if(counter==49)
         pthread_mutex_unlock(&lock); 
    
      }
      else
     {
     //the rest of the threads do smth
      }
    }
     int main()
    {
      while (counter<49)
       {
         if(threads_running<5)
         { 
           pthread_create(&thread[counter],NULL,thread,thread_arg)
           counter+=1;
           threads_running+=1;
         }
        }
    
    }