From cb014e58e7b2cc546d276c6885ea79257400724b Mon Sep 17 00:00:00 2001 From: YuQing <384681@qq.com> Date: Fri, 13 Mar 2026 15:06:01 +0800 Subject: [PATCH] test_pthread_lock.c changed --- src/tests/test_pthread_lock.c | 86 ++++++++++++++++++++++++----------- 1 file changed, 59 insertions(+), 27 deletions(-) diff --git a/src/tests/test_pthread_lock.c b/src/tests/test_pthread_lock.c index 62719bc..deeb529 100644 --- a/src/tests/test_pthread_lock.c +++ b/src/tests/test_pthread_lock.c @@ -28,16 +28,68 @@ #include "fastcommon/fast_allocator.h" #define LOOP_COUNT (30 * 1000 * 1000) +#define THREAD_COUNT 2 #define barrier() __asm__ __volatile__("" ::: "memory") +static volatile int64_t sum; +static pthread_mutex_t lock; + +static void *atomic_thread_func(void *arg) +{ + int k; + for (k=1; k<=LOOP_COUNT; k++) { + //__sync_synchronize(); + //barrier(); + __sync_add_and_fetch(&sum, k); + } + + return NULL; +} + +static void *mutex_thread_func(void *arg) +{ + int k; + for (k=1; k<=LOOP_COUNT; k++) { + pthread_mutex_lock(&lock); + sum += k; + pthread_mutex_unlock(&lock); + } + + return NULL; +} + +typedef void *(*thread_func)(void *arg); + +static int test(const char *caption, thread_func thread_run) +{ + int64_t start_time; + char time_buff[32]; + pthread_t tids[THREAD_COUNT]; + int i; + int result; + + start_time = get_current_time_ms(); + sum = 0; + + for (i=0; i