国际访客建议访问 Primers 编程伙伴 国际版站点 > C 教程 > thrd_equal 以获得更好的体验。

# C 语言标准库函数 thrd_equal

在头文件 threads.h 中定义。
/*********************************************
 * @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)

本文 更新于: 2025-11-27 09:38:11 创建于: 2025-11-27 09:38:11