Thread race condition sample code
This commit is contained in:
26
Practice_06/raceCondition.c
Normal file
26
Practice_06/raceCondition.c
Normal file
@@ -0,0 +1,26 @@
|
||||
#include <stdio.h>
|
||||
#include <pthread.h>
|
||||
|
||||
int counter = 0; //공유변수
|
||||
|
||||
void* increment(void* arg){
|
||||
for(int i = 0; i < 10000000; i++){
|
||||
counter++;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int main(){
|
||||
pthread_t t1, t2;
|
||||
|
||||
pthread_create(&t1, NULL, increment, NULL);
|
||||
pthread_create(&t2, NULL, increment, NULL);
|
||||
|
||||
pthread_join(t1, NULL);
|
||||
pthread_join(t2, NULL);
|
||||
|
||||
printf("Counter : %d\n", counter);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user