From: Dmitry Antipov <dantipov@nvidia.com>
To: GDB Development <gdb@sourceware.org>
Subject: Printing thread_local a.k.a. __thread variables
Date: Mon, 12 Feb 2018 13:30:00 -0000 [thread overview]
Message-ID: <780169f5-fa63-3357-ed78-a3045991c307@nvidia.com> (raw)
[-- 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;
}
next reply other threads:[~2018-02-12 13:30 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-02-12 13:30 Dmitry Antipov [this message]
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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=780169f5-fa63-3357-ed78-a3045991c307@nvidia.com \
--to=dantipov@nvidia.com \
--cc=gdb@sourceware.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox