__thread??

Does ‘__thread’ work on pgi?
I’m attempting to compile below sample codes, but it fails.

#include <pthread.h>

__thread int a = 0;

void * func(void *arg)
{
a++;
printf(“a=%d\n”, a);
}

int main()
{
pthread_t x;

pthread_create(&x, NULL, func, NULL);
pthread_join(x, NULL);
}

pgcc -lpthread test.c

test.c:
PGC-S-0040-Illegal use of symbol, __thread (test.c: 3)
PGC-W-0118-Function func does not contain a return statement (test.c: 9)
PGC/x86-64 Linux 7.1-4: compilation completed with severe errors

So, should we have to rewrite the code to use ‘__thread’ or is there any solutions or options? if so, how?

Hi si2009,

Unfortunately, we don’t yet support thread local storage so you’ll need to use pthread keys. I’m sure you can find better tutorials elsewhere (I’m not an expert with pthreads), but I’ve pulled together a small example where I create a key, initialize it in the ‘func’ function, and then use it in the ‘seta’ function.

% cat pt.c
#include <pthread.h>
#include <malloc.h>

pthread_key_t a;
int zero = 0;

void seta () {
  int * a_tmp = (int *) malloc( sizeof(int));
  a_tmp = (int *) pthread_getspecific(a);
  *a_tmp += 10;
  pthread_setspecific(a, (void *) a_tmp);

}

void * func(void *arg)
{
  int * a_tmp = (int *) malloc(sizeof(int));
  *a_tmp = 1;
  printf("Before Set A=%d\n", *a_tmp);
  pthread_setspecific(a, (void *) a_tmp);
  seta();
  printf("After Set A=%d\n", *a_tmp);
  return NULL;
}

int main()
{
pthread_t x;

pthread_key_create(&a, NULL);
pthread_create(&x, NULL, func, NULL);
pthread_join(x, NULL);
}
% pgcc pt.c -lpthread
% a.out
Before Set A=1
After Set A=11
  • Mat