Operating System - Linux
1751809 Members
4669 Online
108781 Solutions
New Discussion юеВ

Re: thread-local storage doesnt work with gcc

 
SOLVED
Go to solution
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