为什么我不能直接比较2个线程ID而不是使用pthread_equal? [英] Why cannot I directly compare 2 thread ids instead of using pthread_equal?

查看:52
本文介绍了为什么我不能直接比较2个线程ID而不是使用pthread_equal?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道Linux中有一个系统调用pthread_equal用于比较2个线程ID.但是,为什么不能使用'=='运算符直接比较2个线程ID?

I know there is a system call pthread_equal in Linux for comparing 2 thread ids. But why cannot one directly compare 2 thread ids using '==' operator?

推荐答案

在Linux上的pthread_equal手册页中:

From the pthread_equal man page on Linux:

pthread_equal()函数是必需的,因为线程ID应该被认为是不透明的:应用程序没有可移植的方式来直接比较两个pthread_t值.

The pthread_equal() function is necessary because thread IDs should be considered opaque: there is no portable way for applications to directly compare two pthread_t values.

它可能是一个结构.它可能是一个指针.它可能是指向某个地方保存的结构的指针. == 可能会或可能不会在所有情况下都返回true,应该返回true,反之亦然.

It might be a struct. It might be a pointer. It might be a pointer to a struct held somewhere. == might, or might not, return true for all cases it should return true and vice versa.

因此,无论实现如何,都为您提供了一个保证返回正确结果的访问器.

So you are provided with an accessor that is guaranteed to return the correct result, no matter the implementation.

这篇关于为什么我不能直接比较2个线程ID而不是使用pthread_equal?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆