Home > Mobile >  How to get size_t value out of std::thread::id on Windows?
How to get size_t value out of std::thread::id on Windows?

Time:05-17

How to get size_t value out of std::thread::id on Windows?

The thread-id is 9120 (id and this_id). I tried few the ANSI C way, that resulted in a deafferent id:

enter image description here

Code:

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 not size_t)

    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());
}

Update:

@Chnossos suggestion works as expected:

enter image description here

CodePudding user response:

There is an operator<< available:

#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;
}

Try it online

  • Related