如何擺脫std::thread::id與“Win API thread-id”相同的 id(在 Windows 上)?
執行緒 ID 為 9120(id和this_id)。我嘗試了一些 ANSI C 方法,但它們產生了不同的 id:

代碼:
int main()
{
// Win API:
const auto id = Concurrency::details::platform::GetCurrentThreadId(); // OK
// ANSI C :
const std::thread::id this_id = std::this_thread::get_id(); // OK (but internal only: _id)
constexpr std::hash<std::thread::id> myHashObject{};
const auto threadId1 = myHashObject(std::this_thread::get_id());
const auto threadId2 = std::hash<std::thread::id>{}(std::this_thread::get_id());
const auto threadId3 = std::hash<std::thread::id>()(std::this_thread::get_id());
}
更新:
@Chnossos 建議按預期作業:

uj5u.com熱心網友回復:
您不應該依賴格式或值(請參閱下面的評論)。話雖這么說,你可以玩operator<<:
#include <iostream>
#include <sstream>
#include <thread>
int main()
{
std::stringstream ss;
ss << std::this_thread::get_id();
std::size_t sz;
ss >> sz;
std::cout << std::this_thread::get_id() << " vs. " << sz << std::endl;
}
在線嘗試
輸出不可移植,并且不能保證轉換或轉換格式。不要在生產代碼中使用它。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/477623.html
