vendredi 3 juillet 2015

How to traverse all thread_local variables?

I would like to update a variable which is altered by multiple threads.

I could use a single atomic variable, and atomically increment it, but it is allegedly slow.

A proposed solution has been to allocate a thread-local variable, and increment it.

thread_local unsigned int _tls_size = 0U;
/* ... */
_tls_size++;
/* ... */

This step is ok, but the problem is now to calculate the sum of all _tls_size stored across threads.

Any ideas ? I thought about having a std::vector<unsigned int *> storing all addresses, but :

  • Visual Studio didn't like it and returned errors.
  • What happens when a thread detachs ? The corresponding pointer will not be referencing valid memory !

Thank you !

Aucun commentaire:

Enregistrer un commentaire