Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::hash<std::thread::id>

      From cppreference.com
      <cpp‎ |thread‎ |thread‎ |id
       
       
      Concurrency support library
      Threads
      (C++11)
      (C++20)
      this_thread namespace
      (C++11)
      (C++11)
      (C++11)
      Cooperative cancellation
      Mutual exclusion
      Generic lock management
      (C++11)
      (C++11)
      (C++11)
      (C++11)
      Condition variables
      (C++11)
      Semaphores
      Latches and Barriers
      (C++20)
      (C++20)
      Futures
      (C++11)
      (C++11)
      (C++11)
      Safe reclamation
      Hazard pointers
      Atomic types
      (C++11)
      (C++20)
      Initialization of atomic types
      (C++11)(deprecated in C++20)
      (C++11)(deprecated in C++20)
      Memory ordering
      (C++11)(deprecated in C++26)
      Free functions for atomic operations
      Free functions for atomic flags
       
       
      std::thread::id
      Member functions
      Non-member functions
      (until C++20)(until C++20)(until C++20)(until C++20)(until C++20)(C++20)
      Helper classes
      hash<std::thread::id>
       
      Defined in header<thread>
      template<>struct hash<std::thread::id>;
      (since C++11)

      The template specialization ofstd::hash for thestd::thread::id class allows users to obtain hashes of the identifiers of threads.

      [edit]Example

      This section is incomplete
      Reason: example that uses hash meaningfully
      Run this code
      #include <chrono>#include <iostream>#include <thread>#include <vector>usingnamespace std::chrono_literals; void foo(){std::this_thread::sleep_for(10ms);} int main(){std::vector<std::thread> v;for(int n=0; n<4;++n)        v.emplace_back(foo); std::hash<std::thread::id> hasher;for(auto& t: v){std::cout<<"thread "<< t.get_id()<<" hashes to "<< hasher(t.get_id())<<'\n';        t.join();}}

      Possible output:

      thread 139786440144640 hashes to 8905351942358389397thread 139786431751936 hashes to 9222844670065909738thread 139786423359232 hashes to 18199000599186780501thread 139786414966528 hashes to 15386662774029264672

      [edit]See also

      (C++11)
      hash function object
      (class template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/thread/thread/id/hash&oldid=161280"

      [8]ページ先頭

      ©2009-2025 Movatter.jp