打印 std::this_thread::get_id() 得到 "thread::id of a non-executing thread"?
Printing std::this_thread::get_id() gives "thread::id of a non-executing thread"?
这曾经工作得很好(然后外星人一定黑了我的电脑):
#include <thread>
#include <iostream>
int main()
{
std::cout << std::this_thread::get_id() << std::endl;
return 0;
}
现在它打印 thread::id of a non-executing thread
。
ideone.com 打印了一些 ID,但有趣的是是什么导致了我平台上的这种行为。
$ uname -a
Linux xxx 3.13.0-77-generic #121-Ubuntu SMP Wed Jan 20 10:50:42 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
有什么想法吗?
编辑:嗯..当我添加
std::cout << pthread_self() << std::endl;
两行 打印相同的 ID,但是当我删除它时,结果仍然相同 - "non-executing thread".
标准实际上并没有定义 this_thread::get_id
将变成 return。它只说:
Returns: An object of type thread::id that uniquely identifies the
current thread of execution. No other thread of execution shall have
this id and this thread of execution shall always have this id. The
object returned shall not compare equal to a default constructed
thread::id.
你的输出满足这个条件,所以一切正常。
顺便说一下,不要混淆 thread_id
returned by this_thread::get_id
和数字线程 id returned by std::thread::get_id()
。 thread_id
主要用法是在关联容器中进行比较和使用,而不是直接自省或打印。
这是 glibc 功能的副作用,已在 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=57060 中修复:
// For the GNU C library pthread_self() is usable without linking to
// libpthread.so but returns 0, so we cannot use it in single-threaded
// programs, because this_thread::get_id() != thread::id{} must be true.
如果您显式link反对 pthreads(-pthread
或 -lpthread
),那么您的程序将按预期运行。
奇怪的是,在我的系统上,添加对 pthread_self
的调用(在调用 std::this_thread::get_id()
之前或之后不会改变行为:
0
thread::id of a non-executing thread
这可能是 Ubuntu 特有的行为,如果 pthread_self
被调用,link 会自动创建 pthreads,但这看起来有点奇怪。请注意,std::this_thread::get_id()
通过弱引用调用 pthread_self
(本身通过 __gthread_self
)。
这曾经工作得很好(然后外星人一定黑了我的电脑):
#include <thread>
#include <iostream>
int main()
{
std::cout << std::this_thread::get_id() << std::endl;
return 0;
}
现在它打印 thread::id of a non-executing thread
。
ideone.com 打印了一些 ID,但有趣的是是什么导致了我平台上的这种行为。
$ uname -a
Linux xxx 3.13.0-77-generic #121-Ubuntu SMP Wed Jan 20 10:50:42 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
有什么想法吗?
编辑:嗯..当我添加
std::cout << pthread_self() << std::endl;
两行 打印相同的 ID,但是当我删除它时,结果仍然相同 - "non-executing thread".
标准实际上并没有定义 this_thread::get_id
将变成 return。它只说:
Returns: An object of type thread::id that uniquely identifies the current thread of execution. No other thread of execution shall have this id and this thread of execution shall always have this id. The object returned shall not compare equal to a default constructed thread::id.
你的输出满足这个条件,所以一切正常。
顺便说一下,不要混淆 thread_id
returned by this_thread::get_id
和数字线程 id returned by std::thread::get_id()
。 thread_id
主要用法是在关联容器中进行比较和使用,而不是直接自省或打印。
这是 glibc 功能的副作用,已在 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=57060 中修复:
// For the GNU C library pthread_self() is usable without linking to
// libpthread.so but returns 0, so we cannot use it in single-threaded
// programs, because this_thread::get_id() != thread::id{} must be true.
如果您显式link反对 pthreads(-pthread
或 -lpthread
),那么您的程序将按预期运行。
奇怪的是,在我的系统上,添加对 pthread_self
的调用(在调用 std::this_thread::get_id()
之前或之后不会改变行为:
0
thread::id of a non-executing thread
这可能是 Ubuntu 特有的行为,如果 pthread_self
被调用,link 会自动创建 pthreads,但这看起来有点奇怪。请注意,std::this_thread::get_id()
通过弱引用调用 pthread_self
(本身通过 __gthread_self
)。