我正在学习线程约3周。我现在要练习在C中创建3个线程并测量线程在ns中创建和启动的时间。显然我使用QueryPerformanceCounter进行测量。
...
__int64 Time_Create, Time_Start, freq, start, start2,create, end;
//Time_Create to measure when the thread created.
//Time_Start to measure the time start.
...
QueryPerformanceFrequency(&freq);
QueryPerformanceCounter(&start);
QueryPerformanceCounter(&start2);
...
for (int i = 0; i<3; i++)
{...
QueryPerformanceCounter(&create);
//Creating threads
hArray_Of_Thread_Handle[i] = CreateThread(
...
QueryPerformanceCounter(&end);
Time_Create = ((create - start) * 1000000) / freq;
Time_Start = ((end - start2) * 1000000) / freq;
printf("Thread %d created at TC %d ns and started at TS %d ns \n" , i+1 , Time_Create, Time_Start);
}
....
输出是什么:
Thread 1 created at TC 3 ns and started at TS 0 ns.
Thread 2 created at TC 1145 ns and started at TS 0 ns.
Thread 3 created at TC 1190 ns and started at TS 0 ns.
我的Time_Start总是0,这是错误的。 所以我想知道是否可以使用多个QueryPerformanceCounter?或者我误解了这个练习?或者我的方法是错误的?
感谢阅读和帮助。
编辑: 答案:将%d更改为%I64d,以便可以打印__int64。