Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TSAN: unexpected deadlock detection. #1646

Open
JackyWoo opened this issue Apr 26, 2023 · 0 comments
Open

TSAN: unexpected deadlock detection. #1646

JackyWoo opened this issue Apr 26, 2023 · 0 comments

Comments

@JackyWoo
Copy link

JackyWoo commented Apr 26, 2023

Env

clang -v
Ubuntu clang version 15.0.7
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/10
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/11
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/9
Selected GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/11
Candidate multilib: .;@m64
Selected multilib: .;@m64

Description

  1. no deadlock, but report potential deadlock
/ not deadlock, but report potential deadlock
void notDeadlockButReport()
{
    std::mutex m1;
    std::mutex m2;

    // m1 => m2
    std::cout << "t try to acquire m1\n";
    m1.lock();
    std::cout << "t got m1\n";

    //std::this_thread::sleep_for(std::chrono::microseconds (2)); // wait main got m2

    // code never executed
    std::cout << "t try to acquire m2\n";
    m2.lock();
    std::cout << "t got m2\n";

    m2.unlock();
    m1.unlock();

    // m2 => m1
    std::cout << "main try to acquire m2\n";
    m2.lock();
    std::cout << "main got m2\n";

    // code never executed
    std::cout << "main try to acquire m1\n";
    m1.lock();
    std::cout << "main got m1\n";

    m1.unlock();
    m2.unlock();
}
  1. deadlock but not report
void deadlockButNotReport()
{
    std::mutex m1;
    std::mutex m2;

    std::thread t([&] {

        std::cout << "t try to acquire m1\n";
        m1.lock();
        std::cout << "t got m1\n";

        std::this_thread::sleep_for(std::chrono::milliseconds (20)); // wait main got m2

        std::cout << "t try to acquire m2\n";
        m2.lock();
        std::cout << "t got m2\n"; // code never executed

        m2.unlock();
        m1.unlock();
    });

    std::this_thread::sleep_for(std::chrono::milliseconds(10)); // wait t got m1

    std::cout << "main try to acquire m2\n";
    m2.lock();
    std::cout << "main got m2\n";

    std::cout << "main try to acquire m1\n";
    m1.lock();
    std::cout << "main got m1\n"; // code never executed

    m1.unlock();
    m2.unlock();

    t.join();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant