public:
typedef _Mutex mutex_type;
+ [[__nodiscard__]]
explicit lock_guard(mutex_type& __m) : _M_device(__m)
{ _M_device.lock(); }
+ [[__nodiscard__]]
lock_guard(mutex_type& __m, adopt_lock_t) noexcept : _M_device(__m)
{ } // calling thread owns mutex
: _M_device(0), _M_owns(false)
{ }
+ [[__nodiscard__]]
explicit unique_lock(mutex_type& __m)
: _M_device(std::__addressof(__m)), _M_owns(false)
{
: _M_device(std::__addressof(__m)), _M_owns(false)
{ }
+ [[__nodiscard__]]
unique_lock(mutex_type& __m, try_to_lock_t)
: _M_device(std::__addressof(__m)), _M_owns(_M_device->try_lock())
{ }
+ [[__nodiscard__]]
unique_lock(mutex_type& __m, adopt_lock_t) noexcept
: _M_device(std::__addressof(__m)), _M_owns(true)
{
}
template<typename _Clock, typename _Duration>
+ [[__nodiscard__]]
unique_lock(mutex_type& __m,
const chrono::time_point<_Clock, _Duration>& __atime)
: _M_device(std::__addressof(__m)),
{ }
template<typename _Rep, typename _Period>
+ [[__nodiscard__]]
unique_lock(mutex_type& __m,
const chrono::duration<_Rep, _Period>& __rtime)
: _M_device(std::__addressof(__m)),
class scoped_lock
{
public:
+
+ [[nodiscard]]
explicit scoped_lock(_MutexTypes&... __m) : _M_devices(std::tie(__m...))
{ std::lock(__m...); }
+ [[nodiscard]]
explicit scoped_lock(adopt_lock_t, _MutexTypes&... __m) noexcept
: _M_devices(std::tie(__m...))
{ } // calling thread owns mutex
public:
using mutex_type = _Mutex;
+ [[nodiscard]]
explicit scoped_lock(mutex_type& __m) : _M_device(__m)
{ _M_device.lock(); }
+ [[nodiscard]]
explicit scoped_lock(adopt_lock_t, mutex_type& __m) noexcept
: _M_device(__m)
{ } // calling thread owns mutex
--- /dev/null
+// { dg-do compile { target c++11 } }
+
+#include <mutex>
+
+struct Mutex
+{
+ void lock();
+ void unlock();
+ bool try_lock();
+};
+
+using namespace std;
+
+void
+test_nodiscard(Mutex& m)
+{
+ lock_guard<Mutex>{m}; // { dg-warning "ignoring return value" }
+ lock_guard<Mutex>{m, adopt_lock}; // { dg-warning "ignoring return value" }
+ lock_guard<Mutex>(m, adopt_lock); // { dg-warning "ignoring return value" }
+}
--- /dev/null
+// { dg-do compile { target c++17 } }
+// { dg-require-gthreads "" }
+// { dg-additional-options "-pthread" { target pthread } }
+
+#include <mutex>
+
+struct Mutex
+{
+ void lock();
+ void unlock();
+ bool try_lock();
+};
+
+using namespace std;
+
+void
+test_nodiscard(Mutex& m, mutex& m2)
+{
+ scoped_lock<>{}; // no warning
+ scoped_lock<>(); // no warning
+
+ scoped_lock{m}; // { dg-warning "ignoring return value" }
+ scoped_lock{adopt_lock, m}; // { dg-warning "ignoring return value" }
+ scoped_lock(adopt_lock, m); // { dg-warning "ignoring return value" }
+ scoped_lock(m, m2); // { dg-warning "ignoring return value" }
+ scoped_lock{m, m2}; // { dg-warning "ignoring return value" }
+ scoped_lock{adopt_lock, m, m2}; // { dg-warning "ignoring return value" }
+ scoped_lock(adopt_lock, m, m2); // { dg-warning "ignoring return value" }
+}
--- /dev/null
+// { dg-do compile { target c++11 } }
+
+#include <mutex>
+#include <chrono>
+
+using namespace std;
+
+struct Mutex
+{
+ void lock();
+ void unlock();
+ bool try_lock();
+
+ bool try_lock_for(chrono::seconds);
+ bool try_lock_until(chrono::system_clock::time_point);
+};
+
+
+void
+test_nodiscard(Mutex& m)
+{
+ unique_lock<Mutex>(); // no warning
+ unique_lock<Mutex>{}; // no warning
+ unique_lock<Mutex>(m, defer_lock); // no warning
+ unique_lock<Mutex>{m, defer_lock}; // no warning
+
+ unique_lock<Mutex>{m}; // { dg-warning "ignoring return value" }
+ unique_lock<Mutex>{m, try_to_lock}; // { dg-warning "ignoring return value" }
+ unique_lock<Mutex>(m, try_to_lock); // { dg-warning "ignoring return value" }
+ unique_lock<Mutex>{m, adopt_lock}; // { dg-warning "ignoring return value" }
+ unique_lock<Mutex>(m, adopt_lock); // { dg-warning "ignoring return value" }
+
+ chrono::seconds reltime(1);
+ unique_lock<Mutex>{m, reltime}; // { dg-warning "ignoring return value" }
+ unique_lock<Mutex>(m, reltime); // { dg-warning "ignoring return value" }
+ chrono::system_clock::time_point abstime(reltime);
+ unique_lock<Mutex>{m, abstime}; // { dg-warning "ignoring return value" }
+ unique_lock<Mutex>(m, abstime); // { dg-warning "ignoring return value" }
+}
+