posix semaphores posting for more than one thread at a time
Linux - SoftwareThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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!
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.