侧边栏壁纸
博主头像
noerror

虚灵不寐,众理具而万事出。

  • 累计撰写 239 篇文章
  • 累计创建 9 个标签
  • 累计收到 2 条评论
标签搜索

目 录CONTENT

文章目录

pthread_getcpuclockid函数用法详解

noerror
2022-10-04 / 0 评论 / 0 点赞 / 155 阅读 / 682 字 / 正在检测是否收录...

pthread_getcpuclockid函数用法详解

pthread_getcpuclockid函数简介

  • 头文件包含
#include <pthread.h>
#include <time.h>
  • 函数定义
int pthread_getcpuclockid(pthread_t  thread , clockid_t * clockid );
  • 编译链接选项
-pthread

pthread_getcpuclockid函数常见使用错误

  • 链接错误
    undefined reference to `pthread_getcpuclockid'
    解决办法:添加链接选项
-pthread
  • 编译错误
    warning: implicit declaration of function ‘pthread_getcpuclockid’ [-Wimplicit-function-declaration]
    解决办法:包含头文件
#include <pthread.h>
#include <time.h>

pthread_getcpuclockid函数详细描述

pthread_getcpuclockid ()函数获取线程的CPU时间时钟ID,线程的ID在thread 中给出,并在clockid 指向的位置返回它

pthread_getcpuclockid函数返回值

成功时,此函数返回0;出错时,它返回一个非零错误号。

pthread_getcpuclockid函数错误码

  • 系统不支持 ENOENT 每线程CPU时间时钟。
  • 找不到ID为thread的线程。

pthread_getcpuclockid函数其他说明

当thread引用调用线程时,此函数返回一个标识符,当给定时钟ID CLOCK_THREAD_CPUTIME_ID 时,该标识符引用由clock_gettime (2)和clock_settime (2)操作的相同时钟

pthread_getcpuclockid函数使用举例

下面的程序创建一个线程,然后使用clock_gettime (2)来检索总进程CPU时间,以及两个线程消耗的每个线程CPU时间。下面的shell会话显示了一个示例运行:

$ \fB./a.out\fP
Main thread sleeping
Subthread starting infinite loop
Main thread consuming some CPU time...
Process total CPU time:    1.368
Main thread CPU time:      0.376
Subthread CPU time:        0.992

Program source&

/* Link with "\-lrt" */

#include <time.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <string.h>
#include <errno.h>

#define handle_error(msg) \e
       do { perror(msg); exit(EXIT_FAILURE); } while (0)

#define handle_error_en(en, msg) \e
       do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)

static void *
thread_start(void *arg)
{
   printf("Subthread starting infinite loop\en");
   for (;;)
       continue;
}

static void
pclock(char *msg, clockid_t cid)
{
   struct timespec ts;

   printf("%s", msg);
   if (clock_gettime(cid, &ts) == \-1)
       handle_error("clock_gettime");
   printf("%4jd.%03ld\en", (intmax_t) ts.tv_sec, ts.tv_nsec / 1000000);
}

int
main(int argc, char *argv[])
{
   pthread_t thread;
   clockid_t cid;
   int s;

   s = pthread_create(&thread, NULL, thread_start, NULL);
   if (s != 0)
       handle_error_en(s, "pthread_create");

   printf("Main thread sleeping\en");
   sleep(1);

   printf("Main thread consuming some CPU time...\en");
   for (int j = 0; j < 2000000; j++)
       getppid();

   pclock("Process total CPU time: ", CLOCK_PROCESS_CPUTIME_ID);

   s = pthread_getcpuclockid(pthread_self(), &cid);
   if (s != 0)
       handle_error_en(s, "pthread_getcpuclockid");
   pclock("Main thread CPU time:   ", cid);

   /* The preceding 4 lines of code could have been replaced by:
      pclock("Main thread CPU time:   ", CLOCK_THREAD_CPUTIME_ID); */

   s = pthread_getcpuclockid(thread, &cid);
   if (s != 0)
       handle_error_en(s, "pthread_getcpuclockid");
   pclock("Subthread CPU time: 1    ", cid);

   exit(EXIT_SUCCESS);         /* Terminates both threads */
}
0

评论区