当前位置: C语言 -- 标准库 -- <threads.h> -- thrd_equal

thrd_equal函数


概要:
#include <threads.h>
int thrd_equal(thrd_t thr0, thrd_t thr1);

描述:

该函数确定参数thr0标识的线程与参数thr1标识的线程是否为同一线程。


参数:
thrd_t thr0

线程标识符。

thrd_t thr1

线程标识符。


返回值:

如果参数thr0标识的线程与参数thr1标识的线程是同一线程,函数返回非0值;如果不是同一线程,函数返回0


范例:
1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 
28 
29 
30 
31 
32 
33 
34 
35 
36 
37 
38 
39 
40 
/*函数thrd_equal范例*/

#ifdef __STDC_NO_THREADS__
#error "Implementation does not support multi-threads."
#endif

#include <stdio.h>
#include <stdlib.h>
#include <threads.h>

/*新线程中执行的函数。*/
int func(void *arg)
{
    /*执行代码。*/
    
    thrd_exit(0);
}

int main(void)
{
    thrd_t threadId;
    
    /*创建线程。*/
    if(thrd_create(&threadId, func, NULL) != thrd_success)
    {
        perror("thrd_create error");
        exit(EXIT_FAILURE);
    }

    /*连接线程。*/
    thrd_join(threadId, NULL);
    
    if(thrd_equal(threadId, thrd_current()))
        puts("The identifier of the current thread is threadId.");
    else
        puts("The identifier of the current thread is not threadId.");
    
    return 0;
}


输出:

The identifier of the current thread is not threadId.

注:使用Pelles C编译。


相关内容:
thrd_current 标识调用线程的函数。
thrd_detach 分离线程的函数。
thrd_sleep 暂停当前线程的函数。
thrd_exit 终止当前线程的函数。
thrd_join 连接线程的函数。
thrd_create 创建线程的函数。
thrd_yield 让其它线程运行的函数。