jeudi 2 août 2018

How to enumerate instances of a thread_local variable

I am using a thread local variable like :

static thread_local     int m_counter;

for a random number sequence counter. Using thread_local works very well, i have one instance per thread.

My problem is that i need to reset these for a new run of my simulation. I do not have a list of the threads, so i do not have access to each instance. I want to systematically reset all counters to 0.

Is there a recommended way (in C++ 11 ) to do this ?

My only idea as of now is to use 2 sets of counters like:

static thread_local     int m_counter_a;   // counter "a"
static thread_local     int m_counter_b;   // counter "b"

Instead of reseting centrally i pass an a,b value to the multithreaded code. Then the counters of the "other" set are reset. This works, but i do repeated resets like 50000 times

if (a) {
   m_counter_b = 0;
   m_counter_a++;
} else {
   m_counter_a = 0;
   m_counter_b++;
}

Is there a way to avoid the repeated resets ?

Aucun commentaire:

Enregistrer un commentaire