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

Add way to get index of thread in its threadpool #57242

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

IanButterworth
Copy link
Member

A proposal to make it easier to index a thread within a thread pool.

i.e. take the following that breaks if there's an interactive threadpool because both nthreads() and @spawn default to the :default pool, which comes second.

x = zeros(nthreads())
Threads.@spawn x[Threads.threadid()] = 1

This introduces

x = zeros(nthreads())
Threads.@spawn x[Threads.threadpool_threadidx()] = 1

Needs consideration for whether this helps the bigger picture of avoiding task migration sensitive design issues.

@IanButterworth IanButterworth added the multithreading Base.Threads and related functionality label Feb 3, 2025
@gbaraldi

This comment was marked as resolved.

@IanButterworth IanButterworth force-pushed the ib/threadpool_threadidx branch from 0012980 to 0f9a54d Compare February 3, 2025 15:15
@Seelengrab
Copy link
Contributor

Seelengrab commented Feb 3, 2025

In my opinion, this won't really help the general issue. Similar to how PHP now has three different functions for escaping SQL, without a hard removal people still use the existing functionality. No amount of blog posts or warnings in docstrings is going to (meaningfully) change that, because old examples/APIs have a large amount of inertia.

People are already used to this (dangerous) pattern from other languages, so using threadid in spite of its problems is more or less second nature. Not to mention that a name like threadpool_threadidx is a lot harder to figure out as the "obviously correct way of doing things", since it's 1) longer 2) non-obvious (you need to know about julia threadpools already to even consider it as a potential solution/tab completion) and 3) not obviously making the bad pattern safer.

IMO, we should accept that the API Base.Threads exposes in its entirety, together with the names it uses for everything, is detrimental to the underlying task-based concurrency story.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
multithreading Base.Threads and related functionality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants