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

[REQ] Getting thread ids #126

Closed
klytje opened this issue Nov 6, 2023 · 3 comments
Closed

[REQ] Getting thread ids #126

klytje opened this issue Nov 6, 2023 · 3 comments
Assignees
Labels
enhancement New feature or request

Comments

@klytje
Copy link

klytje commented Nov 6, 2023

I think it'd be useful to be able to get the thread ids of the threads currently in the pool. This would allow us to use thread-local data with a simple std::unordered_map<std::thread::id, T>.

For me this would be useful for evaluating and binning Euclidian distances. Currently I am allocating a new result vector for every job, and then summing all of them at the end. If I had the thread ids, I could instead allocate only a single result vector for each thread, and then sum these at the end. This would dramatically reduce the time spent allocating and summing vectors.

Technically this should already be possible to do by submitting jobs getting the std::this_thread::get_id() once for each thread, but I am not sure how to guarantee that all threads run this job at least once without excessive looping or waiting.

This can be implemented by adding the following to both header files:

    [[nodiscard]] std::vector<std::thread::id> get_thread_ids() {
        std::vector<std::thread::id> thread_ids;
        for (unsigned int i = 0; i < thread_count; ++i){
            thread_ids.push_back(threads[i].get_id());
        }
        return thread_ids;
    }
@klytje klytje added the enhancement New feature or request label Nov 6, 2023
@bshoshany
Copy link
Owner

bshoshany commented Nov 6, 2023

Thanks for opening this issue, @klytje! A similar feature is actually already implemented in v3.6.0, which should be released within a week or two. A new member function get_native_handles() allows you to get a vector of the native handles for each thread. That's generally more useful, because there isn't much you can actually do in standard C++ with the built-in thread IDs.

The downside is that the native handles are not guaranteed to be easily hashable for use in an associative container (although I don't really see why they shouldn't be). However, I am also introducing the option to have a thread initialization function that runs exactly once on each thread when the thread pool is constructed, which should allow you to get the thread IDs manually as you suggested.

For this release I actually considered defining a built-in std::unordered_map for the thread IDs that maps each one to the pool's own thread index, but now I'm thinking that just defining my own version of std::this_thread::get_id() would probably be easier, maybe even using thread_local.

@klytje
Copy link
Author

klytje commented Nov 7, 2023

Yes I read both of those issues before opening this one, actually. I just thought that using handles or manually extracting the thread ids upon creation seemed like a round-about way of doing something quite simple. I suppose you're right that there's not a whole lot you can do with the thread ids except what I'm trying to accomplish here.

@bshoshany
Copy link
Owner

bshoshany commented Nov 9, 2023

In v3.6.0 there will be a function BS::get_thread_index() (I just wrote it today) that allows you to get the index of this thread in the pool, i.e. a number from 1 to get_thread_count() (I start at 1 because 0 means you're in a thread that's not in the pool, but I'm not sure about that, it might change by the time the new version is released). This is much faster and more convenient and than using an std::unordered_map with the value of std::this_thread::get_id(). You can just use a simple array instead. Therefore I'm not sure there's any point in returning the values of std::this_thread::get_id() for each thread in the pool.

However, if you think there is some other use for it that BS::get_thread_index() cannot provide, please let me know what that use is, and I will consider adding a get_thread_ids() function.

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

No branches or pull requests

2 participants