3

I'm looking at the Clang Thread Safety Analysis feature with Clang 7.0

However, I'm getting an error when I attempt to use the PT_GUARDED_BY annotation with shared_ptr data members:

error: 'pt_guarded_by' only applies to pointer types; type here is 'shared_ptr<MyClassType>' [-Werror,-Wthread-safety-attributes]

This is a bit confusing to me, as the documentation says "PT_GUARDED_BY is ... intended for use on pointers and smart pointers.", and even gives an example which uses std::unique_ptr (using basically the same syntax I'm using).

Is std::shared_ptr incompatible with Clang's thread safety analysis, or is there something else going on here? How should I be stating that the pointed-to class shouldn't be accessed without holding a lock on the associated mutex?

(This is on Linux using GCC's libstdc++ standard library, if it matters.)


After some experiments, it looks like this only happens if the shared_ptr is a member variable, not a global.

#include "mutex.h" // The suggested mutex.h file from the Clang Thread Safety Analysis page
#include <memory>

Mutex mu;

std::shared_ptr< int > toplevel PT_GUARDED_BY(mu); // Okay

class Wrapper {
public:
  std::shared_ptr< int > member PT_GUARDED_BY(mu); // Results in error
};

void test() {
  Wrapper wrapped;
  wrapped.member = std::shared_ptr< int >( new int ); // Should be OK
  *wrapped.member = 42; // Should raise warning
}

I'll note that if I swap out unique_ptr for the shared_ptr both the global and the member variable work fine.

2
  • 2
    @ShafikYaghmour Not really related, except on the superficial "both are about shared_ptr and threads" level. My question isn't concerned with efficiency of shared_ptr, or even the intrinsic thread safety guarantees of the shared_ptr class. Instead, it's about the use of shared_ptr with a particular static analysis tool (which happens to be checking thread safety issues). Commented Dec 5, 2018 at 1:59
  • 1
    Reading the docs, it should indeed work. However I do know that libc++ has extra annotations related to it's tools. Have you tried that one instead? Commented Dec 5, 2018 at 7:22

1 Answer 1

0

After playing around a bit with various compilations of Clang and different C++ standard libraries, I think I have this figured out.

It's a bug in Clang 7.0.0 when used in conjunction with (GCC's) libstdc++.

If you use current (4 Dec 2018) trunk Clang (so what will become 8.0.0 eventually) things work as expected with both (GCC's) libstdc++ and (Clang's) libc++.

If you use Clang 7.0.0, things work as expected with (Clang trunk's) libc++, but raises the above error if you use it with (GCC's) libstdc++ ... at least for libstdc++ from the GCC 4.8 - GCC 6.2 range (which is what I had on hand to test.).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.