如何让Google Test检测Linux上的线程数
How to make Google Test detect the number of threads on Linux?
运行使用Google测试框架编写的死亡测试时,会为每个测试生成以下警告:
[WARNING] .../gtest-death-test.cc:789:: Death tests use fork(), which is unsafe
particularly in a threaded context. For this test, Google Test couldn't detect
the number of threads.
是否有一种方法可以使谷歌测试检测Linux上的线程数?
我查看了源代码,结果发现线程数检测只在MacOS X和QNX上实现,而在Linux或其他平台上没有。所以我通过计算/proc/self/task
中的条目数来实现缺失的功能。因为它可能对其他人有用,我把它贴在这里(我也把它发送到谷歌测试组):
size_t GetThreadCount() {
size_t thread_count = 0;
if (DIR *dir = opendir("/proc/self/task")) {
while (dirent *entry = readdir(dir)) {
if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0)
++thread_count;
}
closedir(dir);
}
return thread_count;
}
截至2015年8月25日,谷歌测试在Linux上实现GetThreadCount
:
size_t GetThreadCount() {
const string filename =
(Message() << "/proc/" << getpid() << "/stat").GetString();
return ReadProcFileField<int>(filename, 19);
}
如果您不太关心测试执行时间,一个方便的替代方法是使用:
::testing::FLAGS_gtest_death_test_style = "threadsafe";
相关文章:
- C/C++ Linux 上的多线程服务器/客户端崩溃
- 给定使用 C++ 或 C,我如何测量在 linux 下进行线程切换需要多长时间?可能吗?
- C++ Linux 中的线程无限期地等待
- 无效使用非静态成员函数 c++ 线程 linux
- Linux VM(重型多线程应用程序)的性能改进
- 睡眠:(睡眠或usleep)并没有将我的线程中的所有内容悬挂在Linux中,而是在Windows中悬挂吗?为什么
- 在Linux中减少每个线程内存
- 线程不在 Linux 上终止,但在 Mac 上终止
- 在 Linux 中从单独的单线程进程生成多线程进程时出现问题
- 如何在Linux上为C和C 创建线程安全LIB
- 在 C Linux 中使用三个线程使用信号量同步按顺序打印 3 4 5 50 次
- VS2017 Linux C 线程问题
- 创建线程,但不要立即在 Linux 中运行它
- 检查Linux功能以设置线程优先级
- 在 Linux 上为 Windows 编译多线程应用程序 [ C++ ]
- C /Linux:如何编写使用套接字的线程安全库
- 将终端附加到创建的线程,在 Linux 上C++
- 如何在没有条件变量的情况下阻止线程中的操作,并在Linux中根据信号恢复操作
- Seg 由对 vector (线程 linux) push_back调用而导致的错误
- 多线程Linux应用程序中的互斥