Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/146.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何测量macos中线程的执行时间?_C++_C_Objective C_Multithreading_Macos - Fatal编程技术网

C++ 如何测量macos中线程的执行时间?

C++ 如何测量macos中线程的执行时间?,c++,c,objective-c,multithreading,macos,C++,C,Objective C,Multithreading,Macos,有没有办法测量macos下线程的执行时间?我知道有getrusage函数,但它应该只测量进程时间(在linux下有测量线程时间的扩展,但不幸的是我在MacOs下工作)。我需要测量线程的时间(用户和内核空间中处理时间的总和)。确切的模拟是GetThreadTimes在Windows()下,您可以将第一个参数的RUSAGE\u THREAD传递到getrusage 编辑: 看起来Mac仅支持RUSAGE\u SELF和RUSAGE\u子项 CLOCK\u THREAD\u CPUTIME\u ID是

有没有办法测量macos下线程的执行时间?我知道有
getrusage
函数,但它应该只测量进程时间(在linux下有测量线程时间的扩展,但不幸的是我在MacOs下工作)。我需要测量线程的时间(用户和内核空间中处理时间的总和)。确切的模拟是
GetThreadTimes
Windows
()

下,您可以将第一个参数的
RUSAGE\u THREAD
传递到
getrusage

编辑: 看起来Mac仅支持
RUSAGE\u SELF
RUSAGE\u子项

CLOCK\u THREAD\u CPUTIME\u ID
是另一个选项,它跟踪调用线程的CPU时间。请注意,这是CPU时间,而不是挂钟时间,例如,任何睡眠时间都不会被计算在内

#include <stdio.h>
#include <pthread.h>
#include <time.h>

void* thread_func(void* args)
{
        struct timespec tp;
        ret = clock_gettime(CLOCK_THREAD_CPUTIME_ID, &tp);
        printf("thread elapsed secs: %ld nsecs %ld\n", tp.tv_sec, tp.tv_nsec);
}
int main()
{
        pthread_t thread;
        pthread_create(&thread, NULL, thread_func, NULL);
        pthread_join(thread, NULL);
}
#包括
#包括
#包括
void*thread\u func(void*args)
{
struct-timespec-tp;
ret=clock\u gettime(clock\u线程\u CPUTIME\u ID和tp);
printf(“线程已用秒数:%ld nsecs%ld\n”,tp.tv\u sec,tp.tv\u nsec);
}
int main()
{
pthread\u t线程;
pthread_create(&thread,NULL,thread_func,NULL);
pthread_join(线程,NULL);
}

据我所知,它是linux扩展(不是macos),我可以在macos下找到这个宏的定义?你能举个例子吗?啊,你说得对。看起来mac只支持RUSAGE_SELF或RUSAGE_CHILDREN。你们可以试试clock\u gettime(clock\u THREAD\u CPUTIME\u ID和tp)。我接受了答案。但文档并没有提到cpu时间。是线程在用户和内核空间花费的时间吗?是的,是线程在CPU上运行的累计时间,无论是在内核还是在用户空间。