vendredi 24 juillet 2015

Local static variables for each thread

Lets say I have a class that after initialization creates a thread and runs a method in it, within it declares a static variable:

void method()
{
     static int var = 0;
     var++;
}

If I create more objects of the class, for example 3, then the method will be called 3 times in 3 different threads. After that var will equal 3. How to accomplish the functionality, where each thread has its own static var that is independent of others. I would appreciate all help.

Aucun commentaire:

Enregistrer un commentaire