Intel® oneAPI Threading Building Blocks
Ask questions and share information about adding parallelism to your applications when using this threading library.
2464 Discussions

Making Parallel Sort Comparator Thread Safe

Shant_H_
Beginner
196 Views

Hello,

I'm using parallel_sort and regretfully my comparator needs to use a object that is not thread safe. I can make copies of this object for each thread and then the comparator would just need to use the right copy for the thread it is on, kind of like a thread_local but not global.

I'm looking for some tips on how to achieve this with TBB. I know task_scheduler_observer and enumerable_thread_specific are both options for implementing thread_locals but I'm not sure which is better in my case.

Thanks in advance!

0 Kudos
1 Reply
RafSchietekat
Valued Contributor III
196 Views

task_scheduler_observer is not about thread-local storage.

Just use "tbb::enumerable_thread_specific<Comparator> ets_c(c);", with c your prototype Comparator.

(Added) Oh yes, you still have to pass it to the sort algorithm, without having it copied... probably with a simple wrapper object, with a member variable that's a pointer or reference to ets_c.

0 Kudos
Reply