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

thrd_success枚举常量


概要:
    enum {
        thrd_success = value,   //value值由实现定义。
        thrd_nomem = value,     //value值由实现定义。
        thrd_timedout = value,  //value值由实现定义。
        thrd_busy = value,      //value值由实现定义。
        thrd_error = value      //value值由实现定义。
     };

描述:

该枚举常量由函数返回,表示请求的操作成功执行。


Pelles C编译器<threads.h>头文件中,此类返回代码定义如下:

    enum {
        thrd_success = 0,   
        thrd_error = 1,     
        thrd_busy = 2,  
        thrd_nomem = 3,      
        thrd_timedout = 4      
     };

范例:
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 
41 
42 
43 
/*枚举常量thrd_success范例*/

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

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

#define NUMBER 3

/*新线程中执行的函数。*/
int func(void *data)
{
    printf("Thread %d started.\n", *((int *)data));
    
    return 0;
}

int main(void)
{
    thrd_t threadId[NUMBER];
    int data[NUMBER];
    
    /*创建线程。*/
    for(int i=0; i<NUMBER; ++i)
    {
        data[i] = i+1;
        if(thrd_create((threadId+i), func, (data+i)) != thrd_success)
        {
            perror("thrd_create");
            exit(EXIT_FAILURE);
        }
    }
    
    /*连接线程。*/
    for(int i=0; i<NUMBER; ++i)
        thrd_join(threadId[i], NULL);
    
    return 0;
}


输出:

Thread 1 started.

Thread 2 started.

Thread 3 started.

注:使用Pelles C编译。


相关内容:
thrd_nomem 表示因无法分配内存而操作失败的枚举常量。
thrd_timedout 表示超时的枚举常量。
thrd_busy 表示因请求资源已在使用而操作失败的枚举常量。
thrd_error 表示操作失败的枚举常量。