Skip to content

Instantly share code, notes, and snippets.

@JavaDeveloper
Created October 2, 2012 13:32
Show Gist options
  • Save JavaDeveloper/3819173 to your computer and use it in GitHub Desktop.
Save JavaDeveloper/3819173 to your computer and use it in GitHub Desktop.
unsigned int counter = 0;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
void* thread_method(void* parameter){
string s = *(string*)(parameter);
for(int i=0;i<4;i++){
pthread_mutex_lock(&mutex);
counter++;
cout<<s<<": "<<counter<<endl;
pthread_mutex_unlock(&mutex);
// relinquish the CPU, give a chance to other thread
pthread_yield();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment