As part of a multi-threaded code that I am designing, I am using mutexes to control access to a shared data segments in the code. I have no problems initalising and manipulating the mutexes to control access, however terminating the mutex itself is proving to be a bit difficult.
I am terminating the mutex in the following way
Is this an appropriate way of terminating the mutex ? If not could someone please advise a much better strategy ?
Thanks for your post. In this regard, I was wondering if you could elaborate on it to convince me why I should use pthread_cond_wait(). AFAIK, pthread_cond_wait() causes the mutex lock to be released and blocks the calling thread on a certain condition variable value.
If the calling thread is blocked (which is the thread associated with main() in my case), wouldn't this make it even more difficult to terminate the mutex ?
In your case it would not make sense. It would serialize the program flow which is not what you want probably.
The reason I suggested using pthread_cond_wait (somewhat naively) is that I don't see any locking around your lock_flag variable. If that's the case and multiple threads could access the variable...races abound.
Here's an entirely contrived example for clarity. it's a useless exercise of course.