2
votes

I am confused that what should I use either a mutex or a semaphore in my application,basically my application is a multithreaded server programmed using C and Pthreads. In my application one thread has a dependency over the other i.e one thread needs to wait until a condition is met in the other thread. Earlier I was using the While loop to check when the other thread set the condition as true,but While loop consumes CPU cycles needlessly i.e CPU consumption goes upto 100%.

Currently I started using a mutex in my application as follows:

pthread_mutex_lock(&t_data[rc].mutex);
pthread_mutex_unlock(&t_data[rc].mutex);

In one thread I lock the mutex and when the condition is met in the second thread I unlock it in the second thread( I have handled this doing indexing in a structure,in Which along which other items I have kept a mutex field, each thread is assigned an index when a new client makes a connection ).Using this everything is working fine and CPU consumption of server has came down to 2%.But I have one issue in my mind.

As the definition of mutex says that consider if 10 threads are running and they are sharing a common resource suppose some global variables,so when one thread locks a mutex then other threads cannot access the shared resource until the thread which has locked the mutex releases it.Same will be the case with my application. consider I have 10 active threads 5 threads will lock the mutex turn by turn and other 5 will release the mutex. If a thread has locked the mutex then other 4 threads need to wait until it has been released. so at some point of time a deadlock condition might occur if a thread locks a mutex and it didn't get released then all other threads will keep waiting.

please help me get out of this issue.Being Theoretical it might look awkward but it is a real case scenario.Please go through the question again before giving a downvote.

1
You might want to look into condition variables (computing.llnl.gov/tutorials/pthreads/#ConditionVariables)kfsone
A mutex can only be held by one thread at a time, and the thread that holds it must release it. You should learn how to use a mutex in conjunction with a condition variable and a predicate. That is the most important building block to understand and you can build almost anything you want/need with those parts.David Schwartz
As I have created an array of mutex if I lock a mutex consider mutex_arr[0] then will it lock the access of all other threads to the common code area ? In short did creating a single mutex or an array of mutex make a differenceRamanujam
'a deadlock condition might occur if a thread locks a mutex and it didn't get released then all other threads will keep waiting' that is not a deadlock, it's a bug.Martin James
Without more details, eg. a MCVE, we have no idea of the exact nature of your problem. The way you described it, you app is riddled with UB from misuse of mutex where you shoudlbe using condvars or semaphores. Your app seems to be doomed in its present form, but cannot be sure because no code:(Martin James

1 Answers

2
votes

In your case consider use conditional variable. Make the 5 threads waiting on the condvar, and if the thread is done, signal the condvar if you want one other thread to continue run, or call broadcast to let all other blocking threads continue to run. Checkout these pthreads API:

pthread_cond_wait to wait on the condition
pthread_cond_signal  to signal one thread of the waiters
pthread_cond_broadcast  to signal all threads that are waiting