pthread_setspecific.c [plain text]
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
#include "darwintest_defaults.h"
static pthread_key_t key;
static void *
thread(void * arg)
{
char * msg;
T_LOG("thread %lx here: %s\n", (uintptr_t)pthread_self(), (char *)arg);
msg = malloc(256);
sprintf(msg, "This is thread specific data for %lx\n", (uintptr_t)pthread_self());
T_ASSERT_POSIX_ZERO(pthread_setspecific(key, msg), NULL);
return (arg);
}
static void
grim_reaper(void * param)
{
T_LOG("grim_reaper - self: %lx, param: %lx value: %s", (uintptr_t)pthread_self(), (uintptr_t)param, (char *)param);
free(param);
}
T_DECL(pthread_setspecific, "pthread_setspecific",
T_META_ALL_VALID_ARCHS(YES))
{
void * thread_res;
pthread_t t1, t2;
T_ASSERT_POSIX_ZERO(pthread_key_create(&key, grim_reaper), NULL);
T_ASSERT_POSIX_ZERO(pthread_create(&t1, (pthread_attr_t *)NULL, thread, "thread #1 arg"), NULL);
T_ASSERT_POSIX_ZERO(pthread_create(&t2, (pthread_attr_t *)NULL, thread, "thread #2 arg"), NULL);
T_ASSERT_POSIX_ZERO(pthread_join(t1, &thread_res), NULL);
T_ASSERT_POSIX_ZERO(pthread_join(t2, &thread_res), NULL);
}