This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug libstdc++/78237] New: std::timed_mutex::try_lock_for/until affected by system realtime clock


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=78237

            Bug ID: 78237
           Summary: std::timed_mutex::try_lock_for/until affected by
                    system realtime clock
           Product: gcc
           Version: 6.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: marejde at gmail dot com
  Target Milestone: ---

The following program:

#include <chrono>
#include <iostream>
#include <mutex>
#include <thread>

int main(void)
{
        std::recursive_timed_mutex mutex;

        bool ret = mutex.try_lock_for(std::chrono::seconds(2));
        std::cout << "ret = " << ret << "\n";

        std::thread thread([&mutex]() {
                std::this_thread::sleep_for(std::chrono::seconds(1));
                auto now = std::chrono::steady_clock::now();
                bool ret = mutex.try_lock_until(now +
std::chrono::seconds(60));
                std::cout << "ret = " << ret << "\n";
        });

        thread.join();

        return 0;
}

does not work as expected when modifying the system time. try_lock_until()
returns false immediately when time is advanced passed now + 60s.

Compiling with Clang and libc++ try_lock_until() waits for 60 seconds
regardless of what the system time is changed to.

Looking at the implementation in libstdc++ it looks like it ends up calling
pthread_mutex_timedwait() (that uses CLOCK_REALTIME) while the libc++
implementation implements the methods with a condition variable.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]