NetBSD-Bugs archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
lib/49006: thread specific storage not always initialized to NULL
>Number: 49006
>Category: lib
>Synopsis: thread specific storage not always initialized to NULL
>Confidential: no
>Severity: non-critical
>Priority: low
>Responsible: lib-bug-people
>State: open
>Class: sw-bug
>Submitter-Id: net
>Arrival-Date: Thu Jul 17 06:00:00 +0000 2014
>Originator: Tony Cook
>Release: 6.1.3
>Organization:
>Environment:
NetBSD neso.tony.develop-help.com 6.1.3 NetBSD 6.1.3 (GENERIC) amd64
>Description:
pthread_getspecific() will return non-NULL in some cases in a new thread.
If you:
a) create a key with pthread_key_create() with no desructor,
b) create a thread which initializes the key to non-NULL,
c) join that thread, and
d) create a new thread,
that new thread will have non-NULL for the key.
>How-To-Repeat:
The output of the test program below should always be "result: 0x0".
neso$ cat pthread-specific.c
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
pthread_key_t key;
int x; /* something to point to */
static void *
thread1(void *p) {
pthread_setspecific(key, &x);
return NULL;
}
static void *
thread2(void *p) {
return pthread_getspecific(key);
}
static void
dest(void *p) { /* do nothing */ }
static void
fail(const char *msg) {
fprintf(stderr, "%s\n", msg);
exit(1);
}
int main() {
#ifdef HAVE_DESTRUCTOR
int rs = pthread_key_create(&key, dest);
#else
int rs = pthread_key_create(&key, NULL);
#endif
if (rs)
fail("pthread_key_create");
void *result;
#ifndef ONE_THREAD
pthread_t t1;
if (pthread_create(&t1, NULL, thread1, NULL))
fail("pthread_create(t1)");
if (pthread_join(t1, &result))
fail("pthread_join(t1)");
#endif
pthread_t t2;
if (pthread_create(&t2, NULL, thread2, NULL))
fail("pthread_create(t2)");
result = NULL;
if (pthread_join(t2, &result))
fail("pthread_join(t2)");
printf("result: %p\n", result);
return 0;
}
neso$ gcc -lpthread -opthread-specific pthread-specific.c
neso$ ./pthread-specific
result: 0x601508
neso$ gcc -lpthread -opthread-specific pthread-specific.c -DHAVE_DESTRUCTOR
neso$ ./pthread-specific
result: 0x0
neso$ gcc -lpthread -opthread-specific pthread-specific.c -DONE_THREAD
neso$ ./pthread-specific
result: 0x0
neso$ uname -a
NetBSD neso.tony.develop-help.com 6.1.3 NetBSD 6.1.3 (GENERIC) amd64
>Fix:
Home |
Main Index |
Thread Index |
Old Index