约 1368 字

约 7 分钟

#C 语言标准库函数 thrd_equal

/*********************************************
 * @brief 判断两个线程 ID 是否表示同一线程
 * @param lhs 线程 ID
 * @param rhs 线程 ID
 * @return 是(非 0)否(0)相同
 ********************************************/
int thrd_equal(thrd_t lhs, thrd_t rhs);

说明

判断两个线程 ID 是否表示同一线程。

参数

  • lhs - 要比较的线程 ID
  • rhs - 要比较的线程 ID

返回值

  • 表示同一线程时返回非 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)

更新: 2025/10/25

作者: PlanC

创建: 2025/8/22