<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - Issue with std::lock_guard"
   href="https://bugs.llvm.org/show_bug.cgi?id=40016">40016</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Issue with std::lock_guard
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>new-bugs
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>unspecified
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>Macintosh
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>MacOS X
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>enhancement
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>new bugs
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>freesurfer.rge+llvm@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>htmldeveloper@gmail.com, llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Created <span class=""><a href="attachment.cgi?id=21222" name="attach_21222" title="Repro case">attachment 21222</a> <a href="attachment.cgi?id=21222&action=edit" title="Repro case">[details]</a></span>
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.</pre>
        </div>
      </p>


      <hr>
      <span>You are receiving this mail because:</span>

      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>