bshoshany/thread-pool

[REQ] Get the ID of the thread from the pool

sameeul opened this issue · 1 comments

Describe the new feature

Get some sort of index of the particular thread from the thread-pool.

Code example

One of the use case I have is where the tasks submitted to the threadpool need to perform some memory allocation and deallocation. Since the number of threads are known beforehand, I want to create a memory pool and create a one-on-one map with the executing thread and a buffer from the memory pool.

An example of code that utilizes the suggested feature. Paste or write it between the two code fences.

bs::threadpool th(4);
std::vector<std::vector<int>(some_buff_size)> mem_pool(4);
th.push_task([&mem_pool](){
auto id = get_thread_id(); // will tell me which thread from the pool it is using
auto mem_buf = mem_pool[id];
// and do the computation
});

th.wait_for_tasks();

Sorry about this. It looks like there is a similar issue where it was resolved.