From 3e19b83bbfe31a5598f586f7528c2a2c630f8eda Mon Sep 17 00:00:00 2001 From: AuYang261 <459461160@qq.com> Date: Thu, 19 Oct 2023 00:57:38 +0800 Subject: [PATCH] fix warnings in test app --- apps/c/pthread/basic/main.c | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/apps/c/pthread/basic/main.c b/apps/c/pthread/basic/main.c index 6ee9f1a0f..0827f8e67 100644 --- a/apps/c/pthread/basic/main.c +++ b/apps/c/pthread/basic/main.c @@ -207,22 +207,20 @@ static pthread_key_t p_key; void *specific_func(void *arg) { - pthread_setspecific(p_key, arg); + int *p = (int *)malloc(sizeof(int)); + *p = *(int *)arg; + pthread_setspecific(p_key, p); sleep(1); - void *tmp = pthread_getspecific(p_key); - assert(tmp == arg); - // guarantee that printf 0x5678 before 0x1234 - if (arg == (void *)0x1234) { - // sleep(1); - } + int *tmp = (int *)pthread_getspecific(p_key); + assert(*tmp == *(int *)arg); return NULL; } -long res = 0; +int res = 0; void destr_func(void *arg) { - res += (long)arg; + res += *(int *)arg; // It seems that printf in destr_func will cause deadlock // printf("destr_func: %p\n", arg); } @@ -234,8 +232,9 @@ void test_specific() printf("max_keys = %d, got No.%d\n", max_keys, p_key); pthread_t t1, t2; - pthread_create(&t1, NULL, specific_func, (void *)0x1234); - pthread_create(&t2, NULL, specific_func, (void *)0x5678); + int arg1 = 0x1234, arg2 = 0x5678; + pthread_create(&t1, NULL, specific_func, &arg1); + pthread_create(&t2, NULL, specific_func, &arg2); pthread_join(t1, NULL); pthread_join(t2, NULL); if (res != 0x1234 + 0x5678) {