LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 04-12-2011, 01:04 AM   #1
vlrk
Member
 
Registered: Dec 2008
Posts: 51

Rep: Reputation: 1
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 ( posting to all threads)?

your comments would help in understanding .

regards
 
Old 04-18-2011, 03:45 PM   #2
indelible
LQ Newbie
 
Registered: Aug 2008
Posts: 25

Rep: Reputation: 2
Maybe not semaphore

Hi,

I think that a semaphore is the wrong synchronization primitive to use for this situation. I think you actually want a condition variable. They mean "form an orderly queue behind a single mutex, until the condition is met, then the service will either let one in at a time, or it's a free-for-all" and I think you want the free-for-all option.

see man 3 pthread_cond_init

Then to wake up all the threads waiting on the condition variable, you would use pthread_cond_broadcast like so:


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

pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;

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 = 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)
     {
          pthread_mutex_lock(&mut);
          printf("Enter message to end to thread \n");
          fgets(msg1,MAX_MSG_LEN,stdin);
          pthread_cond_broadcast(&cond);
          pthread_mutex_unlock(&mut);
     }

     return 0;          /* writing the comments */
}

void *thrdFun1(void *arg)
{
    pthread_mutex_lock(&mut);
    /* The condition you're waiting on goes here.  It's almost certainly not a while(1) loop*/
    while (1) {
        pthread_cond_wait(&cond, &mut);
    }
    pthread_mutex_unlock(&mut);
    printf("Iam %s message is %s\n",arg,msg1);
}

I haven't compiled and tried this, so there might be some minor errors (though post back here and I'll see if I can figure them out if you're stuck) and this is also likely to produce garbled output to the terminal, but that's proof that it works as that means more than one thread woke up and started writing to the terminal. Of course, you might get nice output too, there is such a small amount of work for each thread (especially if you're on a single core system), it's possible that each thread will complete in a single scheduler timeslice. That's what sucks about threading - I can't tell you easily ahead of time what kind of output you're going to get, without writing another 100 or so lines of code!


Hth,
Michael
 
Old 04-18-2011, 03:51 PM   #3
indelible
LQ Newbie
 
Registered: Aug 2008
Posts: 25

Rep: Reputation: 2
Of course, I've just realised that this program isn't the best test case... I can't guarantee that all the threads have initialised and are waiting on the condition to be met (that's probably the condition you want to wait on in the threads' worker function). And the while(1) loop in the main thread is going to get stuck forever, so you probably want to get rid of that.

I don't really have time to fix it right now, but as it's an example, I think it at least points you in the right direction. But like I said in the previous post; post any follow-up questions here and I'll see if I can get them resolved.

Michael
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Adding posting date/time information with a thread cola LQ Suggestions & Feedback 2 02-20-2010 03:01 PM
POSIX Semaphores in C endfx Programming 3 02-11-2010 02:56 PM
Problem with POSIX semaphores wej@wejc.com Linux - Software 1 12-16-2007 05:56 PM
POSIX semaphores acting strangely spuzzzzzzz Programming 0 10-18-2004 09:10 PM
POSIX File-based Semaphores dragonman Linux - Newbie 1 11-17-2002 10:51 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 11:40 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration