Thread: oredring thread execution via mutex

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    18

    oredring thread execution via mutex

    I am trying to print i th letter of english by i th thread where i is a natural number one by one in the following program. I want to print A B C D E F... by the thread 1 2 3 4.... How to print the letters A B C D E F by using mutex lock in the following thread program?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <pthread.h>
    #include <semaphore.h>
    #include <unistd.h> 
    pthread_mutex_t mutex;
    int i1=0;
    int m[]={0,1,2,3,4,5};
    void *text(void *arg)
    {
    int *n = (int *)arg;
    if((*n)==i1){
    pthread_mutex_lock(&mutex); 
    i1++;
      switch ((*n))
      { 
        case 0:
          printf("A ");
          break;
        case 1:
          printf("B ");
          break;
        case 2:
          printf("C ");
          break;
        case 3:
          printf("D ");
          break;
        case 4:
          printf("E ");
          break;
        case 5:
          printf("F ");
          break;
      }
    pthread_mutex_unlock(&mutex);
    }
      pthread_exit(0);
    }
    
    int main()
    {
        int i;
    if(pthread_mutex_init(&mutex, NULL)!=0) {printf("mutex initialisation has failed"); return 1;}
      pthread_t tid[6];
        for (i=0;i<6;i++)
        {
            if(pthread_create(&tid[i],NULL,text,(void *)&m[i])!=0) {printf("Thread creation has failed");return 1;}
        }
        for (i=0;i<6;i++) pthread_join(tid[i], NULL);
     pthread_mutex_destroy(&mutex);
        return 0;
    }
    Last edited by mrityunjay23; 01-26-2019 at 12:11 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,416
    Maybe learn to indent code first.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <pthread.h>
    #include <semaphore.h>
    #include <unistd.h>
    pthread_mutex_t mutex;
    int i1 = 0;
    int m[] = { 0, 1, 2, 3, 4, 5 };
    
    void *text(void *arg)
    {
      int *n = (int *) arg;
      if ((*n) == i1) {
        pthread_mutex_lock(&mutex);
        i1++;
        switch ((*n)) {
        case 0:
          printf("A ");
          break;
        case 1:
          printf("B ");
          break;
        case 2:
          printf("C ");
          break;
        case 3:
          printf("D ");
          break;
        case 4:
          printf("E ");
          break;
        case 5:
          printf("F ");
          break;
        }
        pthread_mutex_unlock(&mutex);
      }
      pthread_exit(0);
    }
    
    int main()
    {
      int i;
      if (pthread_mutex_init(&mutex, NULL) != 0) {
        printf("mutex initialisation has failed");
        return 1;
      }
      pthread_t tid[6];
      for (i = 0; i < 6; i++) {
        if (pthread_create(&tid[i], NULL, text, (void *) &m[i]) != 0) {
          printf("Thread creation has failed");
          return 1;
        }
      }
      for (i = 0; i < 6; i++)
        pthread_join(tid[i], NULL);
      pthread_mutex_destroy(&mutex);
      return 0;
    }
    This isn't going to work.
    > if ((*n) == i1)
    Most of your threads are going to fail at the first hurdle.
    You test it outside the mutex, and increment it inside the mutex.

    Even if you make the mutex lock the entire function, you have NO control over which of the 5 threads waiting for the lock runs next, after the first one unlocks it.

    You need condition variables if you want each thread to know when it's its turn to run.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2013
    Posts
    18
    I am trying with condition variables but I am not getting logic how to do with condition variable?
    Remember: m[i] th thread (thread which executing function text(m[i]) is called m[i] th thread) print i th case statement in the i th order.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. thread or mutex SIGTRAP in multiple threads program
    By heatblazer in forum C Programming
    Replies: 2
    Last Post: 04-30-2016, 12:12 AM
  2. Thread synchronization (mutex) not working
    By estov in forum C Programming
    Replies: 4
    Last Post: 07-29-2015, 06:01 AM
  3. Replies: 5
    Last Post: 03-14-2014, 03:15 PM
  4. Mutex, Cond and Thread questions (pthread, linux)
    By thebearot in forum C Programming
    Replies: 14
    Last Post: 04-23-2010, 12:10 PM
  5. Thread/mutex difficulty
    By erasm in forum C Programming
    Replies: 30
    Last Post: 09-21-2009, 06:11 PM

Tags for this Thread