2012-01-21 35 views
0

我正在編寫一個pthread C++程序。這是一個不可移植的pthread C++程序錯誤

我得到錯誤: 「pthread_getunique_np」並沒有在此範圍內

的pthread_getunique_np或其他帶「* NP」,宣佈不可移植。

我找不到在線帖子的解決方案。

我知道_np表示它不是POSIX標準。

如何解決這個問題?我需要包含一些頭文件或其他替代品?

感謝

+1

使用不同的功能...?看看你的更廣泛的目標,並找到不同的解決方案。 –

回答

3

就像馬丁說的,在很多系統上,你可以使用線程的pthread_t作爲唯一標識符。您可以用POSIX的pthread_self(3)來檢索。您可以使用功能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()的信息)

0

_np後綴表示非便攜式的。換句話說,它不是POSIX標準的一部分,因此不能保證在POSIX系統上可用。

+0

謝謝,我知道。我需要知道如何解決它。 – user1002288

0

只需使用pthread_t作爲唯一標識符。無論如何,我很可能與pthread_getunique_np的返回值相同。