Thread: Multithreading help

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    103

    Multithreading help

    Hi All

    I want to write a multithreaded program where I want to print string using 2 threads. ie. if String is:
    Code:
    char  a[] = {"Welcome to the world of Multithreading Programming"};
    Thread 1: Welcome
    Thread 2: To
    Thread 1:The
    Thread 2: World
    and so on.
    I used condition variable to preform this but not working fine.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <pthread.h>
    
    pthread_mutex_t count_mutex     = PTHREAD_MUTEX_INITIALIZER;
    pthread_cond_t  condition_var   = PTHREAD_COND_INITIALIZER;
    
    void *functionCount1();
    void *functionCount2();
    int  count = 0;
    
    main()
    {
        char  a[] = {"Welcome to the world of Multithreading Programming"};
        int i = 0,rc ;
       pthread_t thread1, thread2;
       
       rc = pthread_create( &thread1, NULL, &functionCount1, a);
       pthread_create( &thread2, NULL, &functionCount2, a);
       pthread_join( thread1, NULL);
       pthread_join( thread2, NULL);
    
       exit(0);
    }
    
    void *functionCount1(char *a)
    {
      
       for(;;)
       {  
          pthread_mutex_lock( &count_mutex );
          pthread_cond_wait( &condition_var, &count_mutex );
          while (a[count] != ' ' && a[count] != '\0')
          {
              printf("%c",a[count]);
              count++;
          }
          
          pthread_mutex_unlock( &count_mutex );
    
          if(a[count] == '\0') return(NULL);
        }
    }
    
    void *functionCount2(char *a)
    {
        for(;;)
        {
           pthread_mutex_lock( &count_mutex );
           while (a[count] != ' ' && a[count] != '\0')
          {
              printf("%c",a[count]);
              count++;
          }
          pthread_cond_signal( &condition_var );
          pthread_mutex_unlock( &count_mutex );
    
           if(a[count] == '\0') return(NULL);
        }
    
    }
    When I run this code, nothing is happening.
    The same thing I have used for printing numbers in alternate seq. and it works fine.

    Can any suggest problem with the above code.

    Thanks

  2. #2
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Well, the first problem might be that your for(; loops would never terminate.


    Code:
    if(a[count] == '\0') return(NULL);
    Should probably be

    Code:
    if(a[count] == '\0' || a[count] == ' ') return(NULL);
    But another problem might be that you're doing this comparison after unlocking the mutex. Maybe make a copy of a[count] before unlocking and using that in the comparison. Also consider using isspace() (<ctype.h>) instead of comparing to ' '

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Hey nickman.
    I was able to get it working.
    Here's a clue: Both functions ended up being identical so I ended up using just one.

    And I think SirPrattlepod is a little confused. There's nothing wrong with your loop. It's the way you're handling the condition_var.
    Last edited by oogabooga; 09-18-2013 at 10:43 PM.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  4. #4
    Registered User
    Join Date
    Jun 2010
    Posts
    103
    I have done the same but it print
    Welcome Welcome
    and terminate. Is there any issue with using conditional variable.

  5. #5
    Registered User
    Join Date
    Jun 2010
    Posts
    103
    Thanks oogabooga.
    So you mean using one thread. But I want to do this using 2 threads.

    Thanks

  6. #6
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    No, it uses two threads.
    But each thread runs the same function.

    Output:
    Thread 1: Welcome
    Thread 2: to
    Thread 1: the
    Thread 2: world
    Thread 1: of
    Thread 2: Multithreading
    Thread 1: Programming
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  7. #7
    Registered User
    Join Date
    Jun 2010
    Posts
    103
    here is my code.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <pthread.h>
    
    pthread_mutex_t count_mutex     = PTHREAD_MUTEX_INITIALIZER;
    pthread_cond_t  condition_var   = PTHREAD_COND_INITIALIZER;
    
    void *functionCount1();
    void *functionCount2();
    int  count = 0;
    
    
    main()
    {
        char  a[] = {"Welcome to the world of Multithreading Programming"};
        int i = 0,rc ;
       pthread_t thread1, thread2;
       
       rc = pthread_create( &thread1, NULL, &functionCount1, a);
       pthread_create( &thread2, NULL, &functionCount2, a);
      
       
    
       pthread_join( thread1, NULL);
       pthread_join( thread2, NULL);
    
       exit(0);
    }
    
    void *functionCount1(char *a)
    {
      int temp = 0;
       for(;;)
       {  
          temp = count;
          pthread_mutex_lock( &count_mutex );
          pthread_cond_wait( &condition_var, &count_mutex );
          while (a[temp] != ' ' && a[temp] != '\0')
          {
              printf("%c",a[temp]);
              temp++;
          }
          
          pthread_mutex_unlock( &count_mutex );
          count = temp; 
          
          if(a[count] == '\0' ) return (NULL);
        }
    }
    
    void *functionCount2(char *a)
    {
        int temp = 0;
        for(;;)
        {
           temp = count;
           printf("\nIn F2 %d %d",temp,count); 
           printf("\n");
           pthread_mutex_lock( &count_mutex );
           while (a[temp] != ' ' && a[temp] != '\0')
          {
              printf("%c",a[temp]);
              temp++;
          }
             count = temp;     
             pthread_cond_signal( &condition_var );
             pthread_mutex_unlock( &count_mutex );
          
          
         count = temp;        
         if(a[count] == '\0' ) return(NULL);
        }
    
    }

  8. #8
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Quote Originally Posted by oogabooga View Post
    And I think SirPrattlepod is a little confused. There's nothing wrong with your loop. It's the way you're handling the condition_var.
    I'm not confused at all

    Code:
        for(;;)                                       // do "forever"
    
        {
    
           pthread_mutex_lock( &count_mutex );
           while (a[count] != ' ' && a[count] != '\0') // Let's assume a ' ' character ends the while loop
    
          {
    
              printf("%c",a[count]);
              count++;
    
          }
    
          pthread_cond_signal( &condition_var );
    
          pthread_mutex_unlock( &count_mutex );
    
     
    
           if(a[count] == '\0') return(NULL);              // We get to here, and a[count] == ' ', so this won't terminate the outer for loop...
                                                                                    // I.e. how can the for loop ever end?
    
        }

  9. #9
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You don't need temp.
    And both functions will be identical (except perhaps one will print "Thread 1" and the other "Thread 2").
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  10. #10
    Registered User
    Join Date
    Jun 2010
    Posts
    103
    can you please post the code here so I can check what need's to correct?

    Thanks

  11. #11
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by nickman View Post
    can you please post the code here so I can check what need's to correct?
    Nope. Sorry.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  12. #12
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by SirPrattlepod View Post
    I'm not confused at all
    I didn't mean anything by it buddy!

    The loop will eventually end because (when properly written) control is passed back and forth between the threads, with each thread moving forward to the next space, and eventually the nul will be hit.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  13. #13
    Registered User
    Join Date
    Jun 2010
    Posts
    103
    ok..thats fine. It's yours.

    Ok so you mean to say, that I should not use conditional variable and both thread use same function.
    I tried this but this too not working.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <pthread.h>
    
    pthread_mutex_t count_mutex     = PTHREAD_MUTEX_INITIALIZER;
    pthread_cond_t  condition_var   = PTHREAD_COND_INITIALIZER;
    
    void *functionCount1();
    void *functionCount2();
    int  count = 0;
    
    
    main()
    {
        char  a[] = {"Welcome to the world of Multithreading Programming"};
        int i = 0,rc ;
       pthread_t thread1, thread2;
       
       pthread_create( &thread1, NULL, &functionCount1, a);
       pthread_create( &thread2, NULL, &functionCount1, a);
      
       pthread_join( thread1, NULL);
       pthread_join( thread2, NULL);
    
       exit(0);
    }
    
    void *functionCount1(char *a)
    {
       for(;;)
       {  
          
          pthread_mutex_lock( &count_mutex );
          //pthread_cond_wait( &condition_var, &count_mutex );
          while (a[count] != ' ' && a[count] != '\0')
          {
              printf("%c",a[count]);
              count++;
          }
          
          pthread_mutex_unlock( &count_mutex );
          
          if(a[count] == '\0') return (NULL);
        }
    }

  14. #14
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Okay, now we're getting somewhere.

    But you'll have to use pthread_cond_wait and pthread_cond_signal. Think about what they do (wait blocks the thread, passing control to the other thread, and signal unblocks the blocked thread) and give it a try.

    You'll also have to increment count past the space after the while loop (or it will be stuck on the space). But don't increment it if the '\0' has been hit.

    By the way, I'm not posting the code because it's a policy here to not just give away solutions.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  15. #15
    Registered User
    Join Date
    Jun 2010
    Posts
    103
    Thanks.. I got working now. The issue was I was not incrementing count.
    Last edited by nickman; 09-18-2013 at 11:29 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C# 4 Multithreading Help
    By Mastermosley in forum C# Programming
    Replies: 4
    Last Post: 07-31-2011, 04:14 PM
  2. multithreading in C++
    By manzoor in forum C++ Programming
    Replies: 19
    Last Post: 11-28-2008, 12:20 PM
  3. How to use Multithreading in C++ ??
    By martyr in forum C++ Programming
    Replies: 3
    Last Post: 03-08-2004, 03:13 AM
  4. Multithreading
    By JaWiB in forum Game Programming
    Replies: 7
    Last Post: 08-24-2003, 09:28 PM
  5. multithreading
    By thedumbmutt in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-13-2002, 11:54 AM