[llvm-bugs] [Bug 40016] New: Issue with std::lock_guard

via llvm-bugs llvm-bugs at lists.llvm.org
Thu Dec 13 16:37:25 PST 2018


https://bugs.llvm.org/show_bug.cgi?id=40016

            Bug ID: 40016
           Summary: Issue with std::lock_guard
           Product: new-bugs
           Version: unspecified
          Hardware: Macintosh
                OS: MacOS X
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: new bugs
          Assignee: unassignedbugs at nondot.org
          Reporter: freesurfer.rge+llvm at gmail.com
                CC: htmldeveloper at gmail.com, llvm-bugs at lists.llvm.org

Created attachment 21222
  --> https://bugs.llvm.org/attachment.cgi?id=21222&action=edit
Repro case

I have found a possible bug in the use of std::lock_guard, although it
may simply be a missing warning.

The issue is if a programmer types

std::mutex mtx;
std::lock_guard<std::mutex>(mtx);

when they meant to have the second line read:

std::lock_guard<std::mutex> lg(mtx);

In the first case, the mutex doesn't lock (or possibly only locks for the
duration of the statement).

This first case may be legal C++ (I'm not a language lawyer, so I can't tell),
but at the very least, I think this merits a warning - the programmer probably
meant to lock the mutex for the duration of the current block.

I was compiling with:
-std=c++11 -Wall -Wextra -Wthread-safety -Wthread-safety-verbose

And:
c++ --version
Apple LLVM version 9.1.0 (clang-902.0.39.1)
Target: x86_64-apple-darwin17.7.0
Thread model: posix

I have submitted this bug to Apple (Issue 45790820), but there has been no
action on it.

The attached file test.cpp demonstrates the issue - compare lines 11 and 21.

I expected output like:
$ ./LockTest 
ShowId: 0x70000cb8a000
ShowId: 0x70000cc0d000
RegularGuard: Starting wait
RegularGuard: Ending wait
AnonGuard: Starting wait
AnonGuard: Ending wait
RegularGuard: Starting wait
RegularGuard: Ending wait
AnonGuard: Starting wait
AnonGuard: Ending wait
RegularGuard: Starting wait
RegularGuard: Ending wait
etc.

However, the output I get is:
$ ./LockTest
ShowId: 0x70000cb8e000
ShowId: 0x70000cc11000
RegularGuard: Starting wait
RegularGuard: Ending wait
AnonGuard: Starting wait
RegularGuard: Starting wait
RegularGuard: Ending wait
RegularGuard: Starting wait
RegularGuard: Ending wait
RegularGuard: Starting wait
RegularGuard: Ending wait
RegularGuard: Starting wait
RegularGuard: Ending wait
RegularGuard: Starting wait
RegularGuard: Ending wait
RegularGuard: Starting wait
RegularGuard: Ending wait
RegularGuard: Starting wait
RegularGuard: Ending wait
RegularGuard: Starting wait
RegularGuard: Ending wait
RegularGuard: Starting wait
RegularGuard: Ending wait
RegularGuard: Starting wait
AnonGuard: Ending wait
RegularGuard: Ending wait
AnonGuard: Starting wait
etc.

If I change line 21 to match line 11, then I get the expected output.

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20181214/f28f127b/attachment.html>


More information about the llvm-bugs mailing list