logo

What are Race Conditions?

#include <stdlib.h> #include <stdio.h> #include <pthread.h> int mails = 0; void* routine() { for (int i = 0; i < 1000000; i++) { mails++; // read mails // increment // write mails } } int main(int argc, char* argv[]) { pthread_t p1, p2, p3, p4; if (pthread_create(&p1, NULL, &routine, NULL) != 0) { return 1; } if (pthread_create(&p2, NULL, &routine, NULL) != 0) { return 2; } if (pthread_join(p1, NULL) != 0) { return 3; } if (pthread_join(p2, NULL) != 0) { return 4; } printf("Number of mails: %d\n", mails); return 0; }
Course thumb

The C programming language made simple

Courses with this lesson