pthread_exit停止线程期间的使用情况
pthread_exit usage during stopping the thread
我正在创建一个类似的线程
pthread_create(&mon_thread, NULL, &ClassA::m_thread, this);
运行以下函数
void* ClassA::m_thread(void *arg){
while (!halt_tx) {
.....}
}
在停止期间,我设置 halt_tx = 1 并让线程到达函数的末尾,在析构函数中我调用 join 函数
ClassA::~ClassA()
{
pthread_join(monitor_thread, NULL);
}
我的问题是我是否应该在停止线程时调用 pthread_exit(NULL)。
No.
当 ClassA::m_thread
函数结束时,会隐式调用pthread_exit
函数的返回值作为线程退出状态。
不过,请确保有适当的退货声明。
pthread_exit的手册页说:
Performing a return from the start function of any thread other than the main thread results in an implicit call to pthread_exit()
创建线程对于计算机来说非常耗费资源。 在类中隐式创建线程是一个坏主意,至少没有类的名称,使线程创建非常清晰。 对于我正在处理的一个项目,我有一个 ConsumerThread 类,该类由想要成为使用者的类继承,它启动一个线程并设置一个队列。 开发人员不会实例化一个名为ConsumerThread的类,然后在创建线程时感到惊讶。 但是,开发人员可以创建 ClassA 并且不知道已创建线程。 即使在编写测试代码时,我也非常小心地确保线程创建是显而易见的,因为类名是显而易见的,以防类在弱点时刻泄漏到生产中。
相关文章:
- 如何在没有死锁和/或争用的情况下正确使用 std::mutex C++?
- 在集合的情况下使用emplace_hint的用例是什么?
- 在没有堆的情况下用两种方法构造对象
- 如何在不中断的情况下正确使用C++中的切换用例语句中的Fallthrough
- 给定一个填充无符号字符**的 C 函数,如何在没有中间副本的情况下用数据填充 std::vector
- 在这种情况下,有没有办法用单个解决方案替换两个仅在类型上不同的相似函数?
- 为什么在一种情况下,我会收到带有字符串文字的已弃用转换警告,而在另一种情况下却没有?
- 不能在没有对象的情况下调用成员函数,但我用对象调用函数
- 在不先显式调用析构函数的情况下,在旧对象上使用placement new是否危险
- 无法在前面有多余空格的情况下获取要打印的数组.只有第一个值会用空格打印
- 在可疑的情况下发出叮当警告:函数'foo'可以用属性"noreturn"声明吗?
- 如何在没有任何JavaScript知识的情况下用PNaCl编写C++HTML5应用程序
- 如何在不使用开关大小写的情况下用C++编写菜单驱动的程序
- 在什么情况下,运算符=应该用左值/右值重载而不是复制和交换来实现
- 在没有测试用例的情况下提升单元测试框架
- 在这种情况下,为函数赋值有什么用
- 如何在没有_setmode的情况下用C++输出unicode
- 如何在没有STL的情况下用c++中的Key创建和访问内存块
- 如何在不锁定文件的情况下用C++编写文件
- clang与gcc运行时的区别:用clang构建的c++类模板在没有复制构造函数的情况下崩溃,用复制构造函数构建的gcc