This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
[3.3.1]: thread local storage - what do I need?
- From: "Marshall, Simon" <simon dot marshall at misys dot com>
- To: "'gcc at gcc dot gnu dot org'" <gcc at gcc dot gnu dot org>
- Date: Fri, 5 Sep 2003 14:40:18 +0100
- Subject: [3.3.1]: thread local storage - what do I need?
Hi, I've been trying to use the thread local storage "__thread"
attribute with gcc (i686-pc-linux-gnu/3.3.1) and SuSE 8.2 (Linux
2.4.20-4GB) on a PIII box without any success. The GCC manual says TLS
requires support from ld, ld.so, libc.so and libpthread.so, but I cannot
find out exactly what I need, whether I have it and how I get it.
I have a function defined in file __thread.cpp as:
#include <stdio.h>
#include <pthread.h>
__thread // make "p" thread-local.
pthread_t p = 0;
extern "C" void *foo (void *)
{
int j = 0;
p = pthread_self();
printf ("thread %d starts\n", p); fflush (stdout);
for (int i = 0; i < 10 * 1000 * 1000; i++)
j += i;
printf ("thread %d ends\n", p); fflush (stdout);
return NULL;
}
I build a shared library like this:
gcc -g -D_REENTRANT -fpic -W -c __thread.cpp -o __thread.o # don't
need -ftls-model= here
gcc -shared __thread.o -o lib__thread.so
I then link the library to a file containing main() that creates several
threads running function foo() like this:
gcc -g -D_REENTRANT -fpic -W -L. -l__thread -lpthread __threadmain.cpp
./lib__thread.so: undefined reference to `___tls_get_addr'
collect2: ld returned 1 exit status
Does this mean I don't have the necessary support for TLS? What do I
need to get support for it? If I build without the __thread attribute
it works (except that p has a single value of course). With that
executable I also get:
% ldd ./a.out
libpthread.so.0 => /lib/libpthread.so.0 (0x40022000)
libc.so.6 => /lib/libc.so.6 (0x40073000)
/lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x40000000)
Please can anyone shed any light on this or show me where to look?
Thanks, Simon.
NB: FWIW, if I just build an executable from a file with both main() and
foo(), i.e., not a shared library, then the build is OK but SEGVs at the
call to pthread_self() like this:
% gcc -g -D_REENTRANT -fpic -lpthread thread__.cpp
% ./a.out
Segmentation fault
% ldd ./a.out # Get this!?
not a dynamic executable
% gdb ./a.out
(gdb) run
Starting program: /home/marshals/tmp/a.out
[New Thread 16384 (LWP 9493)]
[New Thread 32769 (LWP 9494)]
[New Thread 16386 (LWP 9495)]
[New Thread 32771 (LWP 9496)]
[New Thread 49156 (LWP 9497)]
[New Thread 65541 (LWP 9498)]
BFD: BFD 2.13.90 20020903 assertion fail libbfd.c:1132
[repeats]
Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 65541 (LWP 9498)]
0x08048635 in foo () at thread__.cpp:12
12 p = pthread_self();