我正在学习pthread_cond_t
并编写了以下旨在永久阻止的代码pthread_cond_wait()
:
// main.cpp
// Intentionally blocks forever.
#include <iostream>
#include <cstring>
#include <cerrno>
#include <pthread.h>
int main( int argc, char* argv[] )
{
pthread_cond_t cond;
if ( pthread_cond_init( &cond, NULL ) )
{
std::cout << "pthread_cond_init() failed: " << errno << "(" << strerror( errno ) << ")" << std::endl;
}
pthread_mutex_t mutex;
if ( pthread_mutex_init( &mutex, NULL ) )
{
std::cout << "pthread_mutex_init() failed: " << errno << "(" << strerror( errno ) << ")" << std::endl;
}
pthread_mutex_lock( &mutex );
pthread_cond_wait( &cond, &mutex );
pthread_cond_destroy( &cond );
return 0;
}
当我第一次编译/执行该程序时,可执行文件没有挂起-它退出了:
>g++ --version
g++ (GCC) 4.8.3 20140911 (Red Hat 4.8.3-7)
Copyright (C) 2013 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
>g++ -g main.cpp && ./a.out
> // <-- Note: returned to bash prompt
接下来我想链接到的libpthread
-而现在可执行雄,符合市场预期:
>g++ -g main.cpp -lpthread && ./a.out
^C
> // <-- Needed to send SIGINT to terminate process
我实际上期望遇到必需pthread
功能的链接错误。当我未明确链接时,为什么没有遇到一个libpthread
?
上面的答案可能使这个问题没有意义,但是当编译时没有显式链接时libpthread
,为什么生成的二进制文件“跳过”或忽略了pthead_cond_wait()
?glibc或某处的pthread函数是否有默认的不执行实现?
当您的进程是多线程时,某些glibc函数需要锁定。为了避免每次都拖入libpthread依赖项,glibc使用weakrefs来引用一堆pthread功能。如果它检测到该功能不可用(由于weakref解决方案而不会导致链接器错误),它将默认将这些操作设置为无操作(这很好,因为如果您没有pthread,则无法是多线程的,则不需要锁定)。该解决方案的结果就是您得到的行为。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句