Skip to content

ThreadExt is highly misleading #30

@CAD97

Description

@CAD97

Specifically, because ThreadExt::get_native_id (and thus all of the other methods) doesn't get the native id, it gets the id of the current thread.

As an example:

let current_thread_id = std::thread::current().get_native_id();
let spawned_thread_id = std::thread::spawn(|| ()).thread().get_native_id();
assert_ne!(current_thread_id, spawned_thread_id); // panics

Or written another way:

let parent_thread = std::thread::current();
let parent_thread_id = parent_thread.get_native_id();
std::thread::spawn(|| {
    assert_eq!(parent_thread_id, parent_thread.get_native_id()); // panics
}).join().unwrap();

Removing the extension trait or adding a check that self == thread::current() is required to not be giving actively misleading results.

std::thread::JoinHandle actually supports getting the raw OS handle on cfg(windows) via AsRawHandle::as_raw_handle and cfg(unix) via JoinHandleExt::as_pthread_t. Perhaps it's not completely unreasonable to suggest that we could get similar extension trait access to the OS handle for std::thread::Thread. Until we do, though, ThreadExt is impossible to implement in the implied way of actually getting the data for the represented thread.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions