Bug 92511

Summary: Review: utilities/cameragui/mtqueue.h use bad locking style
Product: [Applications] digikam Reporter: lan
Component: Import-MainViewAssignee: Digikam Developers <digikam-bugs-null>
Status: RESOLVED INTENTIONAL    
Severity: wishlist    
Priority: NOR    
Version: 0.7.0   
Target Milestone: ---   
Platform: unspecified   
OS: Linux   
Latest Commit: Version Fixed In:
Attachments: Better locking style

Description lan 2004-11-01 11:14:19 UTC
Version:           0.7-beta1 (using KDE 3.2.1, SuSE)
Compiler:          gcc version 3.3.3 (SuSE Linux)
OS:                Linux (i686) release 2.6.5-7.111-default

It use something like this:
{
  mutex.lock();
  do_something();
  mutex.unloack();
}

in case of exception or other error lock will not be unlocked.
Qt suggest better style by using QMutexLocker:
{
  QMutexLocker locker(&mutex);
  do_something();
}

locker lock mutex in constructor and unlock it in the destructor
and as far as it created on the stack it WILL be unlocked.
Patch attached.
Comment 1 lan 2004-11-01 11:14:56 UTC
Created attachment 8121 [details]
Better locking style
Comment 2 lan 2004-11-01 11:17:11 UTC
Forgot to add:
this patch also remove lock() and unlock() methods in mtqueue
and aslo make method head() without argument. These methods are not 
used at now.
Comment 3 Renchi Raju 2004-11-01 18:00:50 UTC
can you give me an example of a case where the mutex will not be unlocked? And digikam doesn't use exceptions. QMutexLocker is just a convenience function for locking and unlocking mutex, so that the programmer doesn't have to remember to unlock the mutex for each scenario.
Comment 4 lan 2004-11-02 03:55:59 UTC
You are right, where are can be any exceptions. That is why original code is working right. I just thought it would be better to make code nicer.
Comment 5 Renchi Raju 2004-11-04 02:41:29 UTC
not using qmutexlocker is not a sin :)