c++ - 是否有获取当前线程的 STL 方式?
问题描述
我目前正在从一个大型应用程序中分解 GLib 函数。
在某些函数中,它使用g_thread_self()
返回指向当前线程的指针的 GLib 函数。
我知道在 STL 中有一个std::this_thread::get_id()
函数,但是因为我有一个成员变量来保存指向线程的指针,所以我希望能够直接保存线程的地址。
这在 C++17 中可行吗?
假设我有一个 MyThread 类(请注明我没有编写此源代码的逻辑):
class MyThread {
public:
void startRoutine(MyThread* other)
{
// ...
other->loop();
// ...
// here, I need to know if my thread is joinable
if (other->_thread && other->_thread->joinable()) {
other->thread = nullptr;
}
}
bool isInsideCurrentThread()
{
// `this_thread()` is the function I'm looking for
return nullptr == _thread || this_thread() == _thread;
};
// ...
private:
std::thread* _thread;
}
解决方案
No, as of C++17 (or 20) there is no standard way to obtain a pointer to a std::thread
instance managing the current thread.
推荐阅读
- c# - 表达式树 - 不包含
- c++ - Binary String to Integer Stack
- c - 是否可以创建一个变量来获取 C 结构中定义的不同值的值?
- swift - 通过拆分获取 HealthKit 上的步行距离
- python - funcparserlib:意外结果
- python - Django:如何减小大小并增加视图的凝聚力
- python - django项目的断言失败
- javascript - 如何使用 Javascript 让 CSS 对象移动?
- shopify - Shopify 购买 js sdk addLineItems null
- c - 为什么 tcsetpgrp() 调用不能按预期工作?