Operating System - Linux
1823143 Members
3531 Online
109647 Solutions
New Discussion юеВ

Re: thread-local storage doesnt work with gcc

 
SOLVED
Go to solution
Frank Mehlhose
Occasional Advisor

thread-local storage doesnt work with gcc

Hi all,
Is there a way to use thread-local storage with gcc compiler on HP-UX 11.11?
I am using gcc 3.3.3 and want to compile some test code:

thread_locals_sub.c:
--------------------------------------------------
#include
#include

static __thread int i;
void *t(void *x) {
for(i = 0; i<100; i++)
printf("%d: %d\n",pthread_self(),i);
return;
}

--------------------------------------------------

CC lets me compile the thread_globals_sub object:
cc -c -o thread_globals_sub.o thread_globals_sub.c -lpthread

But gcc comes up with an error:
gcc -c -o thread_globals_sub.o thread_globals_sub.c -lpthread
thread_globals_sub.c:5: error: thread-local storage not supported for this target

It would be nice if I could make the object with the gcc. Or if there is no way, then it would be nice to know the reason.
14 REPLIES 14
Dennis Handly
Acclaimed Contributor

Re: thread-local storage doesnt work with gcc

>I am using gcc 3.3.3 and want to compile some test code:

As the error message says, you can't, yet.
3.4.3 works, at least on IPF.
Steve Ellcey
Valued Contributor

Re: thread-local storage doesnt work with gcc

With GCC you should use -pthread or -mt options instead of -lpthread. -lpthread will add the pthread library but -pthread will add the library and add the -D_REENTRANT -D_THREAD_SAFE and -D_POSIX_C_SOURCE=199506L macro definitions during the compilation. You still probably need a newer version of GCC than 3.3.3.
Frank Mehlhose
Occasional Advisor

Re: thread-local storage doesnt work with gcc

I have tried it with the -pthread option, but it still does not work.

After vacation I will try to test it with a newer version of gcc.

Steven Schweda
Honored Contributor

Re: thread-local storage doesnt work with gcc

> [...] a newer version of gcc.

They are up to 4.2.1, now. I did better not
building Java, as there seems to be a known
out-of-memory problem in the middle of that,
but otherwise it seemed to go well enough on
11.11 (PA-RISC).
Frank Mehlhose
Occasional Advisor

Re: thread-local storage doesnt work with gcc

I have installed the gcc-4.2.0, but it still doesn't work.
(/opt/hp-gcc-4.2.0/bin/gcc -c -pthread -o thread_globals_sub.o thread_globals_sub.c)

I assume that some of the system librarys are too old, or that the linker has the wrong version. Both, the librarys and the linker have to support TLS. But I'am not sure about it.

I will not update all these items. So I have to compile the object with the cc.

Thanks for trying to help me.
Dennis Handly
Acclaimed Contributor

Re: thread-local storage doesnt work with gcc

>I have installed the gcc-4.2.0, but it still doesn't work.

What do you meant doesn't work? You get the same error message?

>I assume that some of the system libraries are too old, or that the linker has the wrong version. Both, the libraries and the linker have to support TLS.

Static TLS should be supported on any 11.11. Dynamic TLS needs 11.23.
Frank Mehlhose
Occasional Advisor

Re: thread-local storage doesnt work with gcc

>What do you meant doesn't work? You get the same error message?
Yes, its the same error message with gcc 3.3.3 and 4.2.0:
thread_globals_sub.c:5: error: thread-local storage not supported for this target

This is the output of the -v option of gcc 4.2.0:
--------------------------------------------------
XXX@YYY: ./gcc -v
Using built-in specs.
Target: hppa1.1-hp-hpux11.11
Configured with: /tmp/gcc-4.2.0.tar.gz/gcc-4.2.0/configure --host=hppa1.1-hp-hpux11.11 --target=hppa1.1-hp-hpux11.11 --build=hppa1.1-hp-hpux11.11 --prefix=/opt/hp-gcc-4.2.0 --with-gnu-as --without-gnu-ld --with-ld=/usr/ccs/bin/ld --enable-threads=posix --enable-languages=c,c++
Thread model: posix
gcc version 4.2.0
--------------------------------------------------

Dennis Handly
Acclaimed Contributor

Re: thread-local storage doesnt work with gcc

Perhaps this is hinting you need to go to Integrity? ;-)

Steve would have to tell you why TLS doesn't seem to work.
Steve Ellcey
Valued Contributor

Re: thread-local storage doesnt work with gcc

It looks like existing GCC's on the HP web site, including 4.2.1, were built without posix threads being enabled. This is a configuration option specified when GCC is built. The next (4.2.2, 4.3) GCC's will default to having posix threads being enabled by on HPPA 11.*. If you build GCC yourself you could set --enable-threads=posix during configuration and then you would be able to use threads and thread local storage.
Frank Mehlhose
Occasional Advisor

Re: thread-local storage doesnt work with gcc

> It looks like existing GCC's on the HP web site, including 4.2.1, were built without posix threads being enabled.

That's not true. The following code does compile with gcc 4.2.0 and works:

--------------------------------------------------
#include
#include
#define NUM_THREADS 4

static void *Work(void *num);

int main(void) {
int NumberOfThreads = NUM_THREADS;
pthread_t Worker[NUM_THREADS];

int j, ret;
for(j = 0; j < NumberOfThreads; j++) {
ret = pthread_create(&Worker[j], NULL, (void *)&Work, (void *)j);
if(ret) {
fprintf(stderr, "Error while Creating Threads\n");
return -1;
}
}

for(j = 0; j < NumberOfThreads; j++) {
pthread_join(Worker[j], NULL);
}

return 0;
}



static void *Work(void *num) {
int i;
for(i = 0; i < 100; i++) {
int j;
char buf[256]= {'\0'};
for(j = 0; j < (int)num; j++) {
sprintf(buf,"%s\t\t",buf);
}
printf("%s%d\n",buf, i);
}
return;
}
--------------------------------------------------
/opt/hp-gcc-4.2.0/bin/gcc -pthread -o parallel parallel.c && ./parallel
--------------------------------------------------

So Pthreads work on my machine with GCC and CC, but TLS does only work with CC.

MfG
Dennis Handly
Acclaimed Contributor

Re: thread-local storage doesnt work with gcc

>but TLS does only work with cc.

That's what Steve meant, gcc wasn't configured to handle the __thread keyword.
Frank Mehlhose
Occasional Advisor

Re: thread-local storage doesnt work with gcc

Yes, but I thought that --enable-threads=posix enables Posix-Threads (including TLS). This option seems to be set in my version of GCC. Pthreads work in both compilers, instead for TLS.

But maybe there is another Keyword that separately enables TLS-support.
Steve Ellcey
Valued Contributor
Solution

Re: thread-local storage doesnt work with gcc

I did some more investigation and it looks like GCC only allows thread local storage if it sees that the assembler being used also supports it. The 2.17 GNU assembler did not support thread local storage on PA and that is why GCC is not allowing it (even if compiled with --enable-threads=posix. The 2.18 GNU assembler has just recently been released and that assembler does allow thread local storage.
Frank Mehlhose
Occasional Advisor

Re: thread-local storage doesnt work with gcc

I think this answers my question.
Thank You