From 1a15a3dbf685a40083063ed2347d4a237296159c Mon Sep 17 00:00:00 2001 From: foxliver Date: Thu, 25 Sep 2025 16:35:30 +0900 Subject: [PATCH] Producer Consumer sample code --- Practice_07/Producer_Consumer.c | 34 +++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 Practice_07/Producer_Consumer.c diff --git a/Practice_07/Producer_Consumer.c b/Practice_07/Producer_Consumer.c new file mode 100644 index 0000000..55d7efd --- /dev/null +++ b/Practice_07/Producer_Consumer.c @@ -0,0 +1,34 @@ +#include +#include + +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; +} \ No newline at end of file