Q: multiple instance of same object running in multithread, how do i prevent to change a global variable by all threads? I have a class named myConsumer and it has a function getDataFromBuffer(). I create multiple instance of the object consumer1, consumer2, consumer3 from my class. Each consumer objects are running in different thread concurrently. Everything is okay until here. But i want to add something. I have a global variable named MyGlob, I want to increase its value by one, it getDataFromBuffer() function is used by consumer1 in that time. And I want to not change the other threads MyGlob variable. How to prevent the others changing my value?
I summerize my problem:
int MyGlob;

class myConsumer
{
public:
    void getDataFromBuffer();
}

void myConsumer::getDataFromBuffer(){
    if(this function is used by consumer1 )
        myGlob++;
}

I am using pthread library for thread on windows and eclipse cdt
