/*********************************************
* @brief 判断两个线程 ID 是否表示同一线程
* @param lhs 线程 ID
* @param rhs 线程 ID
* @return 是(非 0)否(0)相同
********************************************/
int thrd_equal(thrd_t lhs, thrd_t rhs);
!subtitle:说明
判断两个线程 ID 是否表示同一线程。
!subtitle:参数
lhs - 要比较的线程 ID
rhs - 要比较的线程 ID
!subtitle:返回值
表示同一线程时返回非 0
表示不同线程时返回 0
#include <stdio.h>
#include <threads.h>
// 线程入口函数
int func(void* data)
{
return 0;
}
int main(void)
{
// 创建线程
thrd_t th1, th2;
thrd_create(&th1, func, NULL);
thrd_create(&th2, func, NULL);
// 判断是否相同
if (thrd_equal(th1, th2))
{
printf("th1 和 th2 表示同一线程\n");
}
else
{
printf("th1 和 th2 表示不同线程\n");
}
// 等待线程结束
thrd_join(th1, NULL);
thrd_join(th2, NULL);
return 0;
}
运行结果:
th1 和 th2 表示不同线程
C17 standard (ISO/IEC 9899:2018):
7.26.5.4 The thrd_equal function (p: 280)
C11 standard (ISO/IEC 9899:2011):
7.26.5.4 The thrd_equal function (p: 384)