This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
unsigned int counter = 0; | |
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; | |
pthread_cond_t cond_var = PTHREAD_COND_INITIALIZER; | |
void* thread2_method(void* parameter){ | |
string s = *(string*)(parameter); | |
for(int i=0;i<5;i++){ | |
pthread_mutex_lock(&mutex); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int pthread_yield(void); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
./main | |
thread 2: 1 | |
thread 2: 2 | |
thread 2: 3 | |
thread 2: 4 | |
thread 1: 5 | |
thread 1: 6 | |
thread 1: 7 | |
thread 1: 8 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
./main | |
thread 2: 1 | |
thread 1: 3 | |
thread 1: 4 | |
thread 1: 5 | |
thread 2: 6 | |
thread 2: 7 | |
thread 2: 8 | |
thread 1: 12 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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++){ | |
counter++; | |
cout<<s<<": "<<counter<<endl; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int pthread_join(pthread_t thread, void **thread_return); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int pthread_create(pthread_t * thread, const pthread_attr_t * attr, void * (*start_routine)(void *), void *arg); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
./main | |
thread 1 | |
thread 2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
./main | |
thread 1 | |
thread 2 |