除非链接到 pthreads,否则不会出现死锁?

No deadlock unless linked to pthreads?

为什么创建 std::mutex 死锁实际上不会导致死锁,除非程序链接到 pthreads?

以下内容在与 pthreads 库链接时会死锁,如果未链接 pthreads 则不会死锁。已在 gcc 和 clang 上测试。

// clang++ main.cpp -std=c++14 -lpthread
#include <mutex>
int main() {
    std::mutex mtx;
    mtx.lock();
    mtx.lock();
    return 0;
}

我知道如果没有线程库,您实际上不需要互斥功能,但是编译器是否知道链接到的库?能否以此为基础进行优化?

The following will deadlock when linked with pthreads library and will not deadlock if pthreads is not linked in.

那是因为 std::mutex::lock 的默认实现什么都不做

is the compiler aware of the libraries that are linked in?

否:编译器只是调用 std::mutex::lock 并将 mtx 的地址传递给它。行为不同的是该函数的实现

更新:

To clarify, the implementation is able to change itself depending on if a library has been linked in? Through a macro?

当编译器完成编译时,宏预处理也完成,不会有任何进一步的影响。

也许最好是演示一下。假设你有:

int main() { return foo(); }

你能说说上面程序的执行结果是什么吗?不,你不能,因为你不知道 foo 是做什么的。

假设我现在编译如下:

// foo.c
int foo() { return 0; }

gcc -c foo.c && ar ruv libfoo.a foo.o
gcc main.o -L. -lfoo

现在您可以知道程序将以 0 return 代码退出。

现在假设我还编译了以下内容:

// bar.c
int foo() { abort(); }

gcc -c bar.c && ar ruv libbar.a bar.o

最后,我link一样未修改main.o像这样:

gcc main.o -L. -lbar -lfoo

你能说出生成的程序会做什么吗?

您可以:它会随着 SIGABRT 结束并生成核心转储。

请注意 main.o 没有改变,只有 main.o 被 link 反对的库发生了改变。

这与导致您的原始程序行为不同的机制完全相同,具体取决于它是否 linked 反对 libpthread