Producer Consumer sample code
This commit is contained in:
34
Practice_07/Producer_Consumer.c
Normal file
34
Practice_07/Producer_Consumer.c
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <pthread.h>
|
||||||
|
|
||||||
|
int counter = 5;
|
||||||
|
|
||||||
|
void* producer(void* arg){
|
||||||
|
for(int i = 0; i < 1000000; i++){
|
||||||
|
counter++;
|
||||||
|
}
|
||||||
|
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
void* consumer(void* arg){
|
||||||
|
for(int i = 0; i < 1000000; i++){
|
||||||
|
counter--;
|
||||||
|
}
|
||||||
|
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(){
|
||||||
|
pthread_t t1, t2;
|
||||||
|
|
||||||
|
pthread_create(&t1, NULL, producer, NULL);
|
||||||
|
pthread_create(&t2, NULL, consumer, NULL);
|
||||||
|
|
||||||
|
pthread_join(t1, NULL);
|
||||||
|
pthread_join(t2, NULL);
|
||||||
|
|
||||||
|
printf("최종 Counter 값 : %d\n", counter);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user