Problem with TLS initialization

Manish Makkar mmakkar@india.hp.com
Thu Mar 27 12:28:00 GMT 2003


Hi All,

    While writting a thread-safe application we are facing a problem
with tls initialization.
the initialization is not happening and the variable always have a junk
value.
Here is the program to depict the problem:
----------------------------------------------------------------------------

#include <stdio.h>
#include <pthread.h>
#define MAXTHR  2
static __thread int i = 1; // this initialization to 1 is not happening
thr_func()
{
        printf("i= %d, %d\n", pthread_self(), i);
        i = pthread_self();
        printf("%d, %d\n", pthread_self(), i);
}
main ()
{
        pthread_t thread_id[MAXTHR];
        int i;
        for(i=0;i<MAXTHR;i++)
        {
                pthread_create(&thread_id[i],NULL,thr_func,NULL);
        }
        for(i=0;i<MAXTHR;i++)
        {
                pthread_join(thread_id[i],NULL);
        }
}
------------------------------------------------------------------------------------------------------------

# cc  test.c -lpthread
test.c: In function `main':
test.c:17: warning: passing arg 3 of `pthread_create' from incompatible
pointer type

# ./a.out
i= 8194, 1082469344
8194, 8194
i= 16387, 1090857952
16387, 16387

Please let me know if this is a known problem or am I mising something
here.

# gcc -v
Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/3.2/specs
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man
--infodir=/usr/share/info --enable-shared --enable-threads=posix
--disable-checking --host=i386-redhat-linux --with-system-zlib
--enable-__cxa_atexit
Thread model: posix
gcc version 3.2 20020903 (Red Hat Linux 8.0 3.2-7)

# ll /lib/libc-2.3.1.so
-rwxr-xr-x    2 root     root      1210728 Nov  8 07:18
/lib/libc-2.3.1.so

# ll /lib/libpthread-0.10.so
-rwxr-xr-x    1 root     root        66516 Nov  8 07:18
/lib/libpthread-0.10.so

Thanks and Regards
Manish



More information about the Gcc-bugs mailing list