當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


C語言 pthread_getcpuclockid()用法及代碼示例


pthread_getcpuclockid() 函數返回來自指定線程的 CPU-time 時鍾的時鍾 ID。

用法:

int pthread_getcpuclockid (pthread_t id, clockid_t* clock_id);

參數:此方法接受以下參數:



  • thread:要獲取時鍾ID的線程ID,在調用pthread_create()或pthread_self()時可以得到。
  • clock_id:指向函數可以存儲時鍾 ID 的 clockid_t 對象的指針。

返回值:此方法返回特定線程的 CPU 時鍾時間。

下麵是一些示例來展示 pthread_getcpuclockid() 方法的實現:

範例1:下麵的程序創建一個線程,然後使用 clock_gettime(2) 來檢索總進程 CPU 時間和兩個線程消耗的 per-thread CPU 時間。

C


// C program to implement
// the above approach
#include <errno.h>
#include <pthread.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
  
#define handle_error(msg) \
    do
{
    perror(msg);
    exit(EXIT_FAILURE);
}
while (0)
  
#define handle_error_en(en, msg) \
    do
{
    errno = en;
    perror(msg);
    exit(EXIT_FAILURE);
}
while (0)
  
    static void* thread_start(void* arg)
    {
        printf(
            "Subthread starting infinite loop\n");
        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\n",
           (intmax_t)ts.tv_sec,
           ts.tv_nsec / 1000000);
}
  
// Driver code
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\n");
    sleep(1);
  
    printf(
        "Main thread consuming some CPU time...\n");
  
    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);
  
    // Terminates both threads
    exit(EXIT_SUCCESS);
}

輸出:

$ ./a.out
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

範例2:下麵的程序創建一個進程,然後使用 clock_gettime(2) 來檢索總進程 CPU 時間和兩個進程消耗的父子進程 CPU 時間。

C


// C program to implement
// the above approach
#include <pthread.h>
#include <stdio.h>
#include <time.h>
  
pthread_cond_t cond2;
pthread_condattr_t cond2attr;
  
#define test(clk_id) \
    {                \
    printf("%s:%d\n",
#clk_id, clk_id);
           }
  
static void print_clock(char* msg,
                        clockid_t cid)
{
    struct timespec ts;
  
    printf("%s", msg);
  
    if (clock_gettime(cid, &ts) == -1)
        fprintf(stderr,
                "clock_gettime");
  
    printf("%4ld.%03ld\n",
           ts.tv_sec,
           ts.tv_nsec / 1000000);
}
  
void* test_task_fn(void* unused)
{
    printf("test_task_fn.\n");
  
    for (;;)
        continue;
  
    static int status = 12121;
  
    pthread_exit(&status);
    return NULL;
}
  
// Driver code
int main()
{
    int* pstatus;
    int ret;
    clockid_t clockid = 0;
    pthread_t thread_id;
  
    pthread_create(&thread_id, NULL,
                   test_task_fn, NULL);
  
    printf("Main thread sleeping\n");
    sleep(1);
  
    ret = pthread_getcpuclockid(
        thread_id, &clockid);
    print_clock("  Child", clockid);
  
    ret = pthread_getcpuclockid(
        pthread_self(), &clockid);
    print_clock(" Parent",
                clockid);
    print_clock("Process",
                CLOCK_PROCESS_CPUTIME_ID);
  
    pthread_join(thread_id,
                 (void**)&pstatus);
  
    printf("pstatus = %d\n",
           *pstatus);
  
    return 0;
}

輸出:

[root@localhost pthread]# ./compile.sh pthread_getcpuclockid.c
[root@localhost pthread]# ./a.out
Main thread sleeping
test_task_fn.
Child 0.999
Parent 0.001
Process 1.000




相關用法


注:本文由純淨天空篩選整理自siddheshsagar31082001大神的英文原創作品 pthread_getcpuclockid() function in C with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。