Mirror of the gdb mailing list
 help / color / mirror / Atom feed
* Printing thread_local a.k.a. __thread variables
@ 2018-02-12 13:30 Dmitry Antipov
  2018-02-12 14:02 ` Yao Qi
  0 siblings, 1 reply; 6+ messages in thread
From: Dmitry Antipov @ 2018-02-12 13:30 UTC (permalink / raw)
  To: GDB Development

[-- Attachment #1: Type: text/plain, Size: 214 bytes --]

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

[-- Attachment #2: t-thread.cc --]
[-- Type: text/x-c++src, Size: 1194 bytes --]

#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;
}

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2018-02-13  9:19 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-02-12 13:30 Printing thread_local a.k.a. __thread variables Dmitry Antipov
2018-02-12 14:02 ` Yao Qi
2018-02-12 14:15   ` Dmitry Antipov
2018-02-12 14:54     ` Yao Qi
2018-02-13  7:55       ` Dmitry Antipov
2018-02-13  9:19         ` Yao Qi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox