Thread: one semaphore object for more than one consumer thread.

  1. #1
    Registered User
    Join Date
    Mar 2008
    Location
    India
    Posts
    147

    one semaphore object for more than one consumer thread.

    Iam having a simple c program,

    with one main thread

    4 consumer threds

    all consumebr threads are waiting on the same semaphore object.

    my expectation is when main thread do the sem_post all consumer threads will come out of

    blocked state and start printing the data which has been buffered by the main thread.

    Is this right?

    But in reality it does not unblock all the threads only first thread is coming out of the blocked state

    and printing the result.

    Can any body help me is this!

    Code:
    #include <stdio.h>
    #include <unistd.h>
    #include <errno.h>
    #include <stdlib.h>
    #include <string.h>
    
    #include <pthread.h>
    #include <semaphore.h>
    
    #define MAX_MSG_LEN 256
    
    sem_t sem1;
    
    char msg1[MAX_MSG_LEN] = "1";
    
    void *thrdFun1(void *arg);
    void *thrdFun2(void *arg);
    void *thrdFun3(void *arg);
    void *thrdFun4(void *arg);
    
    int main()
    {
         pthread_t thrd1,thrd2,thrd3,thrd4;
         char argmsg1[] = "Thread1: ";
         char argmsg2[] = "Thread2: ";
         char argmsg3[] = "Thread3: ";
         char argmsg4[] = "Thread4: ";
         int res;
         int thNum;
    
         res = sem_init(&sem1,0,0);
         res = pthread_create(&thrd1, NULL, thrdFun1, argmsg1);
         res = pthread_create(&thrd2, NULL, thrdFun2, argmsg2);
         res = pthread_create(&thrd3, NULL, thrdFun3, argmsg3);
         res = pthread_create(&thrd4, NULL, thrdFun4, argmsg4);
    
         while(1)
         {
              printf("Enter message to end to thread \n");
              fgets(msg1,MAX_MSG_LEN,stdin);
              sem_post(&sem1);
         }
    
         return 0;          /* writing the comments */
    }
    
    void *thrdFun1(void *arg)
    {
         while(1)
         {
              sem_wait(&sem1);
              printf("Iam %s message is %s\n",arg,msg1);
         }
    }
    
    
    void *thrdFun4(void *arg)
    {
         while(1)
         {
              sem_wait(&sem1);
              printf("Iam %s message is %s\n",arg,msg1);
         }
    }
    
    
    void *thrdFun3(void *arg)
    {
         while(1)
         {
              sem_wait(&sem1);
              printf("Iam %s message is %s\n",arg,msg1);
         }
    }
    
    
    void *thrdFun2(void *arg)
    {
         while(1)
         {
              sem_wait(&sem1);
              printf("Iam %s message is %s\n",arg,msg1);

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    I'm not trying to sound harsh, but... Duh? You've initialized the semaphore with a value of zero, meaning only one thread can hold it at once.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    Registered User
    Join Date
    Mar 2008
    Location
    India
    Posts
    147
    it's not dependent on the initialized value from sem_init()

    if i give 0 or 4 its not showing any difference .

    The threads are coming out of blocked state sequentially i.e for thread1 to thread4 after each buffer from the user.

    Does this mean like simultaneously all the threads will not come out of blocked state with the single semaphore .

    can any body give there thoughts..

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Calling sem_post increments the semaphore value by 1.
    Calling sem_wait tries to decrement the semaphore value by 1. If the value is 0, then it blocks.

    If more than one thread is blocked on the semaphore, calling sem_post will release exactly one thread. You have no control over which thread is released.

    So even if you called sem_post 4 times in a row, the first thread may run 4 times, or the first and second threads may run twice, etc...

    If you want to signal all 4 threads to do something at once, then a condition variable would be more appropriate.

    gg

  5. #5
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    Your desired functionality can be achieved by placing a "semaphore bomb" in each of the consumer threads.
    Basically, each time one of the threads wake from their sem_wait(), have them call sem_post() right away, it'll domino through, waking each thread... and finishing with a value of 1, so make sure you reset the semaphore's value back to zero before trying to lock them again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Favourite program
    By progcomputeach in forum General Discussions
    Replies: 18
    Last Post: 08-04-2010, 05:27 AM
  2. Replies: 1
    Last Post: 06-25-2010, 07:14 AM
  3. Object destroy itself?
    By cminusminus in forum C++ Programming
    Replies: 28
    Last Post: 03-27-2008, 01:08 AM
  4. Semaphore Question
    By azamsharp1 in forum C Programming
    Replies: 4
    Last Post: 10-30-2005, 09:01 AM
  5. Question on l-values.
    By Hulag in forum C++ Programming
    Replies: 6
    Last Post: 10-13-2005, 04:33 PM