Thread: posix semaphores posting for more than one thread at a time

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

    posix semaphores posting for more than one thread at a time

    Hello All,

    I would like to know can we post more than one thread at a time.

    I have code like below
    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);
    
    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, thrdFun1, argmsg2);
         res = pthread_create(&thrd3, NULL, thrdFun1, argmsg3);
         res = pthread_create(&thrd4, NULL, thrdFun1, 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);
         }
    }
    here it does accept and sem_post serially to the thread than invoking all the threads at a time.

    Do this kind of behaviour can be achived using posix semaphores ( posting to multiple threads)? or should i use any pthread api like pthread_broadcast etc..

    your comments would help in understanding .

    regards

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> invoking all the threads at a time ... can be achieved using posix semaphores?
    No.

    Using pthread_cond_broadcast(), you can wake all threads that are currently waiting on a condition variable.

    gg

  3. #3
    Registered User
    Join Date
    Mar 2008
    Location
    India
    Posts
    147
    thx codeplug for taking time,

    i missed a basic concept here while posting ..! iam creating the binary semaphore so it can be accessed only one thread at a time.

    Even if i create the sem phore object with 4 tokens.

    It does wait in the first thread it self.

    I think I have to read more about pthreads/semaphores .

    thanx any way ..

  4. #4
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158

  5. #5
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    I also couldn't help but not know exactly what your trying to achieve.
    I would like to know can we post more than one thread at a time.
    am creating the binary semaphore so it can be accessed only one thread at a time.
    That sounds contradictory. Are you wanting each thread to only run one at a time but for each one to run one after another?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with posix semaphores.
    By Dimes in forum C Programming
    Replies: 2
    Last Post: 11-16-2010, 07:53 AM
  2. Posix threads synchronisation with semaphores
    By evariste in forum C Programming
    Replies: 6
    Last Post: 10-26-2010, 03:41 PM
  3. Problems with POSIX semaphores.
    By Volanin in forum Linux Programming
    Replies: 2
    Last Post: 07-13-2007, 02:32 PM
  4. Problem with POSIX semaphores
    By kaseo88 in forum C Programming
    Replies: 7
    Last Post: 06-29-2007, 12:31 PM
  5. posix thread help
    By bomberto in forum C++ Programming
    Replies: 1
    Last Post: 10-15-2006, 02:10 AM