Skip to content

Commit

Permalink
fix warnings in test app
Browse files Browse the repository at this point in the history
  • Loading branch information
AuYang261 committed Oct 23, 2023
1 parent 1739e64 commit 3e19b83
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions apps/c/pthread/basic/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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) {
Expand Down

0 comments on commit 3e19b83

Please sign in to comment.