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

TIME_THREAD_ACTIVE宏


概要:
#include <time.h>
#define TIME_THREAD_ACTIVE value  //value值由具体实现定义。

描述:

该宏为可选宏,会扩展为大于0的整数常量,表示以线程特定活动处理时间为基准(thread-specific active processing time base);线程特定活动处理时间,即调用线程自身消耗的CPU处理时间。

如果实现不支持多线程,实现不应定义宏TIME_THREAD_ACTIVE;如果实现定义了宏TIME_THREAD_ACTIVE,其值应不同于宏TIME_UTC、TIME_MONOTONIC和TIME_ACTIVE。

对于同一线程,宏TIME_THREAD_ACTIVE的值应相同;并且某一线程的宏TIME_THREAD_ACTIVE不应被其它线程用作timespec_get或者timespec_getres函数的base参数。

实现可能会定义以TIME_和一个大写字母开头的其它时间基准宏;但ISO/IEC 9899标准要求:实现必须支持TIME_UTC时间基准,TIME_UTC以外的其它时间基准是可选的。


范例:
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 
44 
45 
46 
47 
48 
49 
50 
51 
52 
53 
54 
55 
56 
57 
58 
59 
60 
61 
62 
63 
64 
65 
66 
67 
68 
69 
70 
71 
72 
73 
74 
75 
76 
77 
78 
/*宏TIME_THREAD_ACTIVE范例*/

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

#include <stdio.h>
#include <stdlib.h>
#include <threads.h>  //<threads.h>头文件包含<time.h>。

/*新线程中执行的函数。*/
int func(void *str)
{
    (void)str;
    
    #ifdef TIME_THREAD_ACTIVE
        struct timespec startTime;
        struct timespec endTime;
        double period;
        
        /*获取开始时间。*/
        if(timespec_get(&startTime,TIME_THREAD_ACTIVE) == 0)
        {
            fprintf(stderr, "timespec_get function failed.\n");
            return 1;
        }
        
        /*模拟工作。*/
        unsigned long long sum = 0;
        for(unsigned long long u=0; u<=1000000000ULL; u++)
        {
            sum += u;
        }
        
        /*获取结束时间。*/
        if(timespec_get(&endTime,TIME_THREAD_ACTIVE) == 0)
        {
            fprintf(stderr, "timespec_get function failed.\n");
            return 1;
        }
        
        /*输出耗时。*/
        period = (double)(endTime.tv_sec - startTime.tv_sec)
                + (double)(endTime.tv_nsec - startTime.tv_nsec)/1E9;
        
        printf("sum = %llu\n", sum);
        printf("period: %.9f seconds.\n", period);
    #else
        puts("The implementation does not define macro TIME_THREAD_ACTIVE.");
    #endif
    
    return 0;  
}

int main(void)
{
    thrd_t threadId;
    
    /*创建新线程。*/
    switch(thrd_create(&threadId, func, NULL))
    {
        case thrd_nomem:
            fprintf(stderr, "No memory could be allocated for the thread requested.\n");
            exit(EXIT_FAILURE);
        case thrd_error:
            fprintf(stderr, "The request of thrd_create could not be honored.\n");
            exit(EXIT_FAILURE);
        default:
            puts("The new thread has been created successfully.");
            break; 
    }
    
    /*连接线程。*/
    thrd_join(threadId, NULL);

    return 0;
}


输出:

The implementation does not define macro TIME_THREAD_ACTIVE.

(注:编译时实现还未支持宏TIME_THREAD_ACTIVE。)


相关内容:
struct timespec 表示时间的结构类型。
timespec_get 设置并保存基于指定时间基准日历时间的函数。
TIME_UTC 表示以UTC时间为基准的整型常量的宏。
TIME_MONOTONIC 表示以单调时间为基准的整型常量的宏。
TIME_ACTIVE 表示以总执行时间为基准的整型常量的宏。


主要参考资料:

1、ISO/IEC 9899:2024