如何测量一组特定线程的 CPU 时间?

How can I measure CPU time of a specific set of threads?

我 运行 C++ 程序在 Linux.

有几个线程池(用于计算,用于 io,用于...等)。

系统调用 clock() 为我提供了一种方法来测量进程的所有 CPU 个内核花费的 CPU 时间。

但是,我想测量 CPU 仅由计算线程池中的线程花费的时间。

如何实现?

谢谢 :D

要获取每个线程的 CPU clock ID,您可以使用:pthread_getcpuclockid 并使用此 CPU clock ID 您可以使用 clock_gettime.

检索当前线程 CPU 时间

以下是演示相同内容的示例代码:

struct timespec currTime;
clockid_t threadClockId;

//! Get thread clock Id
pthread_getcpuclockid(pthread_self(), &threadClockId);
//! Using thread clock Id get the clock time
clock_gettime(threadClockId, &currTime);