How to terminate threads using their thread Id ? c++

Is there any way to terminate a thread using the thread id or even getting a Thread object using a thread id ?

1 answer

  • answered 2018-11-12 19:37 SergeyA

    There is nothing you can do from std::thread::id alone. This is simply a unique representation of all currently running threads.

    It's primary purpose is to be used as an index type for associative containers, so what you can do with it is to put it as a key into std::map or std::unordered_map, and than use it to lookup for std::thread object.

    Once you have found the std::thread object, you can retrieve it's native_handle, and than use OS-specific mechanisms to stop the thread. For example, in Posix that would be pthread_cancel.