pthread condition variables and mutexes?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers pthread condition variables and mutexes?
# 1  
Old 03-07-2008
pthread condition variables and mutexes?

I am trying to understand the exact difference between condition variables and mutexes in thread synchronization ?. I know mutex will control the thread access to shared data and condition variables will be useful for waiting for certain event or condition to occur . But I couldn't understand
why do we need mutex if we want to wait on a condition variable .
My question is on pthread_cond_wait ( ..) <-- which needs a mutex and a condition variable, I have seen one example on using this sub routine .
there he took lock on the mutex first with pthread_mutex_lock and then
he is waiting on pthread_cond_wait ..can somebody explain the logic behind it ? .
# 2  
Old 03-07-2008
Well, it acquires the lock to avoid other threads to use the condition variable which is not yet correctly initialized and thats why you need to hold (amongst other things ?) the lock before any signaling is done ... but i might totally be wrong Smilie
Look and read carefully the man pages, you might find something interesting there!
# 3  
Old 08-30-2008
condition and mutex

The wait requites a mutex parameter and it does the following:
unlock mutex
wait for signal on condition
lock mutex

If you don't lock the mutex before calling wait, the wait will try to unlock a mutex that doesn't have a lock - if it gets past that, you'll end up with a dangling lock after the wait

I recommend the following: https://computing.llnl.gov/tutorials...itionVariables
This User Gave Thanks to everweb For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Understanding Condition variables

I am trying the understand the conditional variable concept. I went through the following site: https://computing.llnl.gov/tutorials/pthreads/#ConditionVariables I understand that condition variables allow threads to synchronize based upon the VALUE of data. When the data acheives a particular... (2 Replies)
Discussion started by: rupeshkp728
2 Replies

2. Programming

pthread()

I have a while loop like so: while (counter (file1)); how can I pass that into a pthread_create()? I was thinking ... while(pthread_create(&path, NULL, counter, file)); is that right? (1 Reply)
Discussion started by: l flipboi l
1 Replies

3. Programming

Condition variables

Hi, I am reading through the pthreads tutorial and had a question on the example they have given for condition variables. Here is the code snippet: This is what the thread waiting on the condition variables doing: pthread_mutex_lock(&count_mutex); while (count<COUNT_LIMIT) { <---... (10 Replies)
Discussion started by: the_learner
10 Replies

4. UNIX for Advanced & Expert Users

pthread

I am so confused about the user threads and kernel threads.Suppose I created a thread using pthread create call in Linux ,whether it will be a user thread or kernel thread.If it user thread,then how its map to kernel thread. I heard about the M:1,M:N,1:1 mapping methods.Which method linux is... (1 Reply)
Discussion started by: sujith4u87
1 Replies

5. Programming

locking mutexes, threads & semahores

What is the difference between mutex_lock and pthread_mutex_lock ? Should I use both when using binary semaphores. Also, what is the difference between using a binary semaphore and a counting semaphore where you wait on the condition variable ? (1 Reply)
Discussion started by: joey
1 Replies

6. Programming

How to wait on multiple semaphores/mutexes

I thought one used the select function to wait on multiple semaphores but according to http://opengroup.org/onlinepubs/007908799/xsh/select.html, the select function is only good for I/O features (like pipes and fifos). How can I block for multiple semaphores? What if I have a mixture of things... (1 Reply)
Discussion started by: siegfried
1 Replies

7. Programming

C++ variable scope and mutexes

I've been wondering if I can make mutexes much easier to use in C++ with creative use of a locking class and variable scope, but I'm not sure if things happen in the order I want. Here's pseudocode for something that could use the class: int someclass::getvalue() { int retval; ... (0 Replies)
Discussion started by: Corona688
0 Replies

8. Programming

pthread.h

hallo 2 al can anyone pls tell me where and how can i find and install the pthread.h lib ? thx :cool: (2 Replies)
Discussion started by: XinU*
2 Replies

9. Programming

pthread

consider if the thread routine returns any void pointer while calling pthread_join, the thread resources are freed and the thread will be terminated when the main thread is exit ,that is my assumption whether it is true how do we find whether the thread is alive or terminated how do we find... (0 Replies)
Discussion started by: MKSRaja
0 Replies

10. Programming

More about Pthread

Can someone point to a link where I can get good info about pthread? thanx.. :) (1 Reply)
Discussion started by: jyotipg
1 Replies
Login or Register to Ask a Question