这是pthread C++程序的不可移植错误吗

Is this a non portable error of a pthread C++ program

本文关键字:可移植 错误 pthread C++ 程序 这是      更新时间:2023-10-16

我正在为pthread C++程序编码。

我出错了:"pthread_getunique_np"未在此范围中声明

pthread_getunique_np或其他带有"*np"的内容是不可移植的。

我无法从网上帖子中找到解决方案。

我知道_np意味着它不是POSIX标准。

如何绕过这个?我需要包括一些头文件或其他替代文件?

感谢

正如Martin所说,在许多系统上,您可以只使用线程的pthread_t作为唯一标识符。您可以使用pthread_self(3)(即POSIX)来检索它。您可以使用函数pthread_equal(3)来测试两个pthread_t的等效性。

pthread_t threadID = pthread_self();
if (pthread_equal(threadID, someOtherID) != 0)
    /* Branch based on being the same thread */
else
    /* Branch based on being different threads */

据我所知,pthread_getunique_np()返回一个唯一的整数ID,这与使用pthread_t作为标识符不同。在许多系统上,从pthread_self(3)和f pthread_getunique_np()返回的值是相同的。事实上,需要pthread_t来获得唯一整数。

无论哪种方式,pthread_self(3)都需要返回调用它的线程的ID,所以我相信您应该能够以您想要的方式使用这个可移植函数。

(有关pthread_getunique_np()的信息,来自IBM)

_np后缀的意思是"不可移植"。换句话说,它不是POSIX标准的一部分,因此不能保证在POSIX系统上可用。

只需使用pthread_t作为唯一标识符。无论如何,我很可能与pthread_getunique_np的返回值相同。