CIS: Lock Leaks

The two most popular threading APIs, the Windows API and pthreads, both have the same basic way of locking and unlocking a mutex — that is, with two separate functions. This leaves the code prone to lock leak: the thread that acquired a lock doesn’t release it because an error occurred.

Lock leak is surprisingly common, and can be a source of deadlocks, among other things. The most common way to cause a lock leak is to not use RAII.

Not using RAII means you can cause a lock leak by simply using return without releasing the lock first. This is usually a violation of whatever coding standards are in place, and might occasionally be picked up by a static analysis tool, but it is much easier to avoid than that: simply always use RAII.

About rlc

Software Analyst in embedded systems and C++, C and VHDL developer, I specialize in security, communications protocols and time synchronization, and am interested in concurrency, generic meta-programming and functional programming and their practical applications. I take a pragmatic approach to project management, focusing on the management of risk and scope. I have over two decades of experience as a software professional and a background in science.
This entry was posted in Common Issues with Synchronization and tagged , . Bookmark the permalink.