bshoshany / thread-pool

BS::thread_pool: a fast, lightweight, and easy-to-use C++17 thread pool library
MIT License
2.21k stars 253 forks source link

[REQ] Possibility of Naming Threads? #156

Closed ClayJay3 closed 1 day ago

ClayJay3 commented 2 days ago

Describe the new feature

I was wondering if it's currently possible or would be possible to name threads so that they would show up more easily identifiable in things like the VSCode debugger.

Code example

On linux using pthreads it can be set like this:

// Function to set thread name
void setThreadName(const char* name) {
    pthread_setname_np(pthread_self(), name);
}

On windows it can be set like this.

// Function to set thread name
void setThreadName(const wchar_t* name) {
    SetThreadDescription(GetCurrentThread(), name);
}

Additional information

An example from inside the VSCode debugger callstack: Screenshot from 2024-11-12 18-42-01

bshoshany commented 1 day ago

Hi @ClayJay3 and thanks for the feature request! I am actually adding some OS-specific features like thread priority and affinity to the upcoming v5.0.0 release, so I will look into adding this too. I don't think it's too useful for use from within tasks in the thread pool itself, since you don't know which thread is going to execute your task anyway, but perhaps you could give the whole thread pool a name like "My Pool" and it will automatically change the thread names to "My Pool 1", "My Pool 2", and so on, and a generic function could be made available so users can use it in non-pool threads. Let me know what you think!

ClayJay3 commented 1 day ago

That's cool with me! I agree, I personally have no need be able to name the individual threads in a thread pool, but being able to assign a name to the entire pool or independently running threads would be nice.