Skip to content

Instantly share code, notes, and snippets.

@seyyedaliayati
Created September 4, 2021 06:21
Show Gist options
  • Save seyyedaliayati/3a4c2791cb95040aa8be55d39231d7a9 to your computer and use it in GitHub Desktop.
Save seyyedaliayati/3a4c2791cb95040aa8be55d39231d7a9 to your computer and use it in GitHub Desktop.
Simple Race Condition Example
#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