thread id question

I am going over c++ concurrency and have question for those well versed in this area. I used simple binary which launches thread and display its status. THere is no IPC communication just CONFIG_NUM_THREAD number of threads running with same function.
In the function, I used std::thread::get_id() to get thread ID but it displays huge numbers. I used linux ps axms to display the status which displays PID/PPID/LWP (lightweight process) column however they dont match?
Do we expect that the threads ID-s returned by std::thread::get_id() expected to return same thread ID displayed under LWP column of ps axms command?

Here is my program output and get_id() values are enclosed in parameters, all starting with 7f...

1
2
3
4
5
6
7
8
9
10
(7f2065be5700)4: Hello CONCURRENT WORLD, sleeping for 21
(7f20651e4700)5: Hello CONCURRENT WORLD, sleeping for 24
(7f20665e6700)3: Hello CONCURRENT WORLD, sleeping for 2f
(7f20647e3700)6: Hello CONCURRENT WORLD, sleeping for 2d
(7f2063de2700)7: Hello CONCURRENT WORLD, sleeping for 2b
(7f2066fe7700)2: Hello CONCURRENT WORLD, sleeping for 2d
(7f20633e1700)8: Hello CONCURRENT WORLD, sleeping for 24
(7f20629e0700)9: Hello CONCURRENT WORLD, sleeping for 20
(7f20679e8700)1: Hello CONCURRENT WORLD, sleeping for 1d
(7f20683e9700)0: Hello CONCURRENT WORLD, sleeping for 15


ps axms partial output:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@httpd-server dev-learn]# ps -eLf | egrep -ir "out|PPID"
UID        PID  PPID   LWP  C NLWP STIME TTY          TIME CMD
root      2066  1804  2066  0   11 01:35 pts/0    00:00:00 ./a.out
root      2066  1804  2067  0   11 01:35 pts/0    00:00:00 ./a.out
root      2066  1804  2068  0   11 01:35 pts/0    00:00:00 ./a.out
root      2066  1804  2069  0   11 01:35 pts/0    00:00:00 ./a.out
root      2066  1804  2070  0   11 01:35 pts/0    00:00:00 ./a.out
root      2066  1804  2071  0   11 01:35 pts/0    00:00:00 ./a.out
root      2066  1804  2072  0   11 01:35 pts/0    00:00:00 ./a.out
root      2066  1804  2073  0   11 01:35 pts/0    00:00:00 ./a.out
root      2066  1804  2074  0   11 01:35 pts/0    00:00:00 ./a.out
root      2066  1804  2075  0   11 01:35 pts/0    00:00:00 ./a.out
root      2066  1804  2076  0   11 01:35 pts/0    00:00:00 ./a.out
root      2078  1857  2078  0    1 01:36 pts/1    00:00:00 egrep -ir out|PPID


Thanks.
Last edited on
No, there is no such expectation.

C++ threads on linux are implemented using the pthread API so std::this_thread::get_id() returns the same value as pthread_self(). I quickly checked the glibc implementation and it seems like the thread id is just a pointer to the thread's stack that has been casted into an integer type.
Last edited on
Topic archived. No new replies allowed.