Printing thread_local a.k.a. __thread variables

Dmitry Antipov <[email protected]>
Newsgroups gmane.comp.gdb.devel
Message-ID <[email protected]>
I just tried the recent git snapshot (on x86 GNU/Linux) and always seeing
100 for 'tlocal' variable. Compiled wit GCC 7.3, -O0 -g3, regardless of
-gdwarf-4 default or -gdwarf-5. Am I doing wrong something?

Dmitry
t-thread.cc (text/x-c++src, 1.2 KB)
#include <vector>
#include <chrono>
#include <thread>
#include <string>
#include <cstdio>
#include <cstring>
#include <cstdlib>

int depth;
thread_local int tlocal = 100;

void
bp1 (int t, int level, int n)
{
  auto r = std::rand () % (t + level + n);
  std::this_thread::sleep_for (std::chrono::milliseconds (r));
  printf ("bp1 in thread %d: %d\n", t, tlocal);  
}

void
bp2 (int t, int level, int n)
{
  auto r = std::rand () % (t + level + n);
  std::this_thread::sleep_for (std::chrono::milliseconds (r));
  printf ("bp2 in thread %d: %d\n", t, tlocal);
}

void
sleeper (int t, int level, int n)
{
  int loop = 0;

  if (++level < depth)
    sleeper (t, level, n);
  else
    while (true)
      {
	if (++loop % 2)
	  bp1 (t, level, n);
	else
	  bp2 (t, level, n);
	tlocal += t + 1;
      }
}

int
main (int argc, char *argv[])
{
  auto max = argc > 1 ? std::atoi (argv[1]) : 8;
  depth = argc > 2 ? std::atoi (argv[2]) : 16;

  std::vector<std::thread *> T;
  for (auto i = 0; i < max; i++)
    {
      auto t = new std::thread (sleeper, i, 0, 1000);
      pthread_setname_np (t->native_handle (), "worker");
      T.push_back (t);
    }

  for (auto &t: T)
    t->join ();

  return 0;
}
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.