Question about pthread_getspecific with jemalloc

Ethan Burns burns.ethan at
Tue Jun 14 14:13:10 PDT 2011

On Tue, Jun 14, 2011 at 1:48 PM, Ethan Burns <burns.ethan at> wrote:
> I am experiencing strange behavior with pthread_getspecific when I
> link with jemalloc.  What happens is, after calling malloc(), the
> value of one of my keys seems to change out from under me.  Here is a
> small example that demonstrates this:
>  I am using jemalloc-2.2.1 on
> Linux.  If I compile the example with gcc (ver 4.4.3) using the
> following command: 'gcc -lpthread main.c' then both calls to
> pthread_getspecific return a NULL pointer as expected.  When I link
> with jemalloc, however, ('gcc -ljemalloc -lpthread main.c') then the
> second call to pthread_getspecific returns some non-NULL value.  Is
> this a known bug or am I doing something wrong?

I have done a bit of digging.  It looks like the call to the macro
ARENA_SET on src/jemalloc.c:794 calls pthread_setspecific on the
arenas_tsd key before the key is actually created on
src/jemalloc.c:800.  The attached patch seems to fix the problem.  I
am not familiar with the code so I am not really sure that this patch
doesn't introduce a new bug but if so, I didn't hit it.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: pthread-key.patch
Type: text/x-patch
Size: 868 bytes
Desc: not available
URL: <>

More information about the jemalloc-discuss mailing list