我有以下测试程序。未定义的引用`pthread_mutex_trylock'
#include <iostream>
#include <cstdlib>
using namespace std;
pthread_mutex_t mymutex = PTHREAD_MUTEX_INITIALIZER;
int main(int argc, char *argv[])
{
int iret;
iret = pthread_mutex_trylock(& mymutex);
cout << "Test2 !!! " << endl;
pthread_mutex_unlock(& mymutex);
return EXIT_SUCCESS;
}
如果我编译它不添加并行线程库我得到为pthread_mutex_trylock未解决的错误的错误,但仅限于功能pthread_mutex_trylock。
如果我和pthread_mutex_trylock取代pthread_mutex_trylock程序编译和运行过程中出现好也没有-lpthread *选项。
如果我添加了-lpthraed选项编译命令都运行良好 这个运行良好:$ G ++ test2.c中-o test2的-lpthread 这种警告没有得到解决:$ G ++ test2.c中-o test2的
实施例误差输出: $克++ test2.c中-o TEST2 /tmp/ccU1bBdU.o:在功能main': test2.c:(.text+0x11): undefined reference to
pthread_mutex_trylock” collect2:LD返回1个退出状态
如果我更换instructi on iret = pthread_mutex_trylock(& mymutex);
with iret = pthread_mutex_lock(& mymutex);程序编译和运行没有错误此外,如果并行线程libarry没有添加到编译命令 我知道这是正确的有未解决的错误,如果我没有使用-lpthread选项,但为什么我不一样未解决的错误也为其他pthread_函数?
我使用Fedora的GCC 4.4.2 12
$ g++ --version
g++ (GCC) 4.4.2 20091222 (Red Hat 4.4.2-20)
Copyright (C) 2009 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.
做一些有关于这个unreference仅pthread_mutex_trylock的含义一些建议吗?
感谢您的帮助,恩佐
这可能是一些特定于平台的行为,但为什么你甚至打扰?你应该与pthread链接,就这样做。 – 2010-01-05 22:36:20
mybe我仍然有学习的愿望:) – enzo2 2010-01-07 19:28:58
尽管如此,这个问题在像我这样的情况下非常有用,在这种情况下,调用'pthread_mutex_lock()'被'pthread_mutex_trylock()'取代,导致事情无缘无故地中断。 – foraidt 2013-06-17 13:56:57