Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Beginner
28 Views

Anything similar to a thread index?

I have a thread unsafe class, which is rather expensive to initialize.
I'd like to create an array with instances of the class, size of array equal to the number of threads.
ThenI should be able to call methods of thearray elementsfrom a parallel_for cycles safely without any locks if each thread accessed it's own array element.
Unfortunately there appears to be no way to get a thread index.

Is there a way to do this somehow?
0 Kudos
5 Replies
Highlighted
28 Views

Look at tbb::enumerable_thread_specific and tbb::combinable classes in TBB 2.2. Those were designed to support the paradigm you want to develop using an array and thread indexes.
0 Kudos
Highlighted
New Contributor I
28 Views

Indeed. And in this thread:

http://software.intel.com/en-us/forums/showthread.php?t=68135

you can even found an actual code to get the ids.

The second thing is: do you really need it? You've written (possibly I misunderstood you) that you traverse the array by a parallel_for. In this case you need no locks nor thread-affinity; chunks of the array will be distributed under the covers.

0 Kudos
Highlighted
Beginner
28 Views

Thanks, Alexey, Bartlomiej. Looks like enumerable_thread_specific is the way to go.

Quoting - Bartlomiej
The second thing is: do you really need it? You've written (possibly I misunderstood you) that you traverse the array by a parallel_for. In this case you need no locks nor thread-affinity; chunks of the array will be distributed under the covers.


I believe I do. I want to reuse my class instances between chunks. Thus I need a way to make sure concurrently processed chunks access different instances.
Or am I missing something?
0 Kudos
Highlighted
28 Views

Quoting - avermel
Thanks, Alexey, Bartlomiej. Looks like enumerable_thread_specific is the way to go.

I believe I do. I want to reuse my class instances between chunks. Thus I need a way to make sure concurrently processed chunks access different instances.
Or am I missing something?

I might tell something obvious here -pardon me if so :) but don't use ETS for storing thread IDs, instead use it to store your thread-specific class instances directly.

Just guessing - are those instances for some temporaries that are expensive to re-create?
0 Kudos
Highlighted
Beginner
28 Views

I might tell something obvious here -pardon me if so :) but don't use ETS for storing thread IDs, instead use it to store your thread-specific class instances directly.
Yes, this was my plan.

Just guessing - are those instances for some temporaries that are expensive to re-create?
That's right.
0 Kudos