Created
September 4, 2021 06:21
-
-
Save seyyedaliayati/3a4c2791cb95040aa8be55d39231d7a9 to your computer and use it in GitHub Desktop.
Simple Race Condition Example
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
#include <pthread.h> | |
int global; | |
pthread_mutex_t mutex; | |
int global_locked; | |
void *foo(void *a) { | |
global++; | |
pthread_mutex_lock(&mutex); | |
global_locked++; | |
pthread_mutex_unlock(&mutex); | |
return 0; | |
} | |
int main() { | |
pthread_t t1, t2; | |
pthread_mutex_t mutex; | |
pthread_create(&t1, 0, foo, 0); | |
pthread_create(&t2, 0, foo, 0); | |
pthread_join(t1, 0); | |
pthread_join(t2, 0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment