Issue 76807
Summary [libc++] Hangs in std::condition_variable_any when used with std::stop_token
Labels libc++
Assignees
Reporter alexbatashev
    The following piece of code appears to work reliably and correctly (or at least the way that I expect it to work) with `libstdc++`, but sporadically hangs with the latest `libc++`:

```c++
#include <thread>
#include <stop_token>
#include <mutex>
#include <condition_variable>
#include <iostream>

class thread {
public:
    thread() {
 thread_ = std::jthread([this](std::stop_token st) {
            while (!st.stop_requested()) {
                std::unique_lock lock{m_};
 cv_.wait(lock, st, []{ return false; });
            }
 });
    }
private:
    std::mutex m_;
 std::condition_variable_any cv_;
    std::jthread thread_;
};

int main() {
    thread t;
    #ifdef _LIBCPP_VERSION
    std::cout << "libc++\n";
    #else
 std::cout << "libstdc++\n";
    #endif
}
```

Results with `libstdc++` and Clang 18. The process exits almost immediately with exit code 0.
![image](https://github.com/llvm/llvm-project/assets/829678/cd3f9a87-87f7-40de-a81d-40c4a55f7d9e)

`libc++` with Clang 18 commit d933b88b71b00461815667d7cd0bb2fecd8606db. Sometimes it works fine, but sometimes it would hang forever until the process is killed from outside.
![image](https://github.com/llvm/llvm-project/assets/829678/ff1e03b8-f3d4-4084-bc76-61ca4fc2fc17)

I also tested with commit 7e186d366d6c7def0543acc255931f617e76dff0 with the same result.

https://godbolt.org/z/hjTheTnon

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to