From 6d255bd9005b07cb52b2548ea1055fee014c4638 Mon Sep 17 00:00:00 2001 From: foxliver Date: Thu, 25 Sep 2025 16:39:28 +0900 Subject: [PATCH] Producer Consumer TAS Sample code --- Practice_07/Producer_Consumer_TAS.c | 34 +++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 Practice_07/Producer_Consumer_TAS.c diff --git a/Practice_07/Producer_Consumer_TAS.c b/Practice_07/Producer_Consumer_TAS.c new file mode 100644 index 0000000..227a78c --- /dev/null +++ b/Practice_07/Producer_Consumer_TAS.c @@ -0,0 +1,34 @@ +#include +#include +#include + +atomic_int counter = 5; //원자적 변수 + +void* producer(void* arg){ + for(int i = 0; i < 1000000; i++){ + atomic_fetch_add(&counter, 1); + } + + return NULL; +} + +void *consumer(void* arg){ + for(int i = 0; i < 1000000; i++){ + atomic_fetch_sub(&counter, 1); + } + + 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