Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pthreads handle (opaque) used instead of thread ID on UNIX #17

Open
IS2511 opened this issue Mar 12, 2024 · 0 comments
Open

pthreads handle (opaque) used instead of thread ID on UNIX #17

IS2511 opened this issue Mar 12, 2024 · 0 comments

Comments

@IS2511
Copy link

IS2511 commented Mar 12, 2024

Problematic code:

unsafe { libc::pthread_self() as usize }

pthread_self() actually returns a handle to the pthread thread object, not the thread id12.

Downsides:

  • Thread IDs are guaranteed to be unique only within a process.2
  • Not portable outside of pthreads calls.2
  • Underlying type of pthread_t is not specified in POSIX, assuming is dubious.23

Suggested solution: syscall(SYS_gettid) or libc::gettid()45.

Footnotes

  1. https://stackoverflow.com/a/27238113

  2. https://www.man7.org/linux/man-pages/man3/pthread_self.3.html#NOTES 2 3 4

  3. Also, in this case it's libc::pthread_t -> libc::c_ulong -> u64, casting to usize is... ID collision is highly unlikely, but not guaranteed.

  4. https://www.gnu.org/software/libc/manual/html_node/Process-Identification.html

  5. https://www.man7.org/linux/man-pages/man2/gettid.2.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant