Difference between condition variable and mutex
I have started reading thread synchronization, after reading about the use of condition variable, i got confused with the use of mutex. Please elaborate me on this with some example. THANKYOU
|
Hello,
AFIK the difference has to do with what constitutes an "atomic" operation, ie the guarantee that a variable change of state will not be interrupted between the original test of the variable, and the subsequent setting of the new state. For example: Using a variable Code:
static int bFlag = 0; Using a mutex is supposed to prevent this problem by insuring that the executing thread is NOT swapped out during the internal test-and-set operation. It does this by running the test-and-set at kernel level and by making sure that the scheduler isn't run until the operation has completed. For example: Using a mutex. Code:
static mutex bFlag = MUTEX_FREE; |
I do not exactly known what you place in the term "condition variable" here, but my guess
is that you refer to e.g. a pthread conditional variable. But to me a mutex and a conditional variable is not the same thing and have different usage. As correctly stated in the previous post mutexes has to do with thread synchronization (simultaneous running threads) and the protection of critical code segments (shared data). A CV on the other hand may come very useful for waking up a sleeping (blocked) thread, e.g. signaling data availability etc. as a tool for simple inter-thread communication. This instead of e.g. using shared memory if the destination thread is supposed to operate short-term and then go back to sleep. Waiting on a CV is a blocking operation so the thread would only consume CPU when there is actually data available to operate on. So, it is a blocking operation which means that it can be compared to a select(2) waiting on data from e.g. a pipe which may serve the same purpose. In my opinion I prefer the pipe for this, but the only real reason for that is that if there are other things to wait for (sockets etc) then the pipe-select better fits the design than to also involve CVs. And, please correct me if I am wrong, a select(2) call can not be issued for a CV since the CV can not be part of the select mask. I have also heard rumors about some pthread implementations that is less "optimal" and select(2) is stated to perform much better in time-critical applications. But then, it is just a rumor so please do not take my word for it. If it is really a concern I would implement both and benchmark them against each other. |
yes as stated above mutex makes operations atomic
that is whatever you need to do inside the thread you can do with a lock on a structure a variable whatever and no other threads can toy with that structure or variable or pointers till you are done. this doesn't have to be condition variables. now if you want to use a condition variable to controll your thread (a flag) and others at the same time you can put a mutex lock on the condition flag like rstewart has shown however if we are looking to do something when another thread set the flag we will only lock it when we check the flag so the other thread can change it unfurtunately you can end up with a bunch of threads doing loops and eating up cpu cycles locking and unlocking and checking conditions like crazy waiting to do stuff. (bad) so you figure you have to use wait but if we try to use wait doing this we could concievable cause a race condition by fluke scheduling like before we tried mutex at all. lets say the thread checks the flag (locks and unlocks the mutex) and it's not set. then the schedular interupts and starts main in the middle of setting the flag. then goes back to the thread and it goes to wait in the wrong place and waits forever. enter GNU to the rescue with Code:
int thread_flag = 0; |
Thank you
Thank you all, for elaborately explainning the difference
I was working over it for long to understand it I think now, i can use CV properly. Ujjwal |
All times are GMT -5. The time now is 06:23 PM. |