【发布时间】:2020-10-02 04:31:46
【问题描述】:
它是一个程序,计算10000的除数作为1000创建的最大线程数。
我认为实时更大并且转动。
n_Threads 为 1000
线程.c
struct tms mytms;
clock_t t1, t2;
if( (t1 = times( &mytms )) == -1 )
{
perror( "times 1" );
exit( 1 );
}
for( int i = 0; i < n_Threads; i++ )
{
if( pthread_create( &tid[i], NULL, &thread_func, NULL ) != 0 ){
fprintf( stderr, "pthread_create error!\n" );
fprintf( stderr, "%s\n", strerror( errno ) );
exit( 1 );
}
}
for( int i = 0; i < n_Threads; i++ )
{
pthread_join( tid[i], NULL );
}
if( (t2 = times( &mytms )) == -1 )
{
perror( "times 2" );
exit( 1 );
}
fprintf( stdout, "%d 의 약 수 의 개 수 : %d \n", MAX_NUMBER, shared_Value );
printf( "Real time : %.5f sec\n", (double)(t2 - t1) / CLK_TCK );
printf( "User time : %.5f sec\n", (double)mytms.tms_utime / CLK_TCK );
printf( "System time : %.5f sec\n", (double)mytms.tms_stime / CLK_TCK );
void* thread_func( void* arg )
{
while(loopValue<=MAX_NUMBER){
pthread_mutex_lock( (&mutex) );
if( MAX_NUMBER % loopValue == 0 ) {
shared_Value++;
}
loopValue++;
pthread_mutex_unlock( (&mutex) );
}
return NULL;
}
结果
10000 number of proper divisor : 25
Real time : 0.40000 sec
User time : 0.09000 sec
System time : 0.59000 sec
为什么系统时间大于实时时间?
【问题讨论】:
-
请编辑您的问题以显示您用于测量时间的确切命令,并发布
uname -a的输出。 -
在转换为
double时可能存在舍入问题:如果您将滴答时钟的数量测量为intmax_t并使用sysconf(_SC_CLK_TCK)转换为秒,输出是什么? -
系统时间计算在内核中花费的总 CPU 时间。如果您有多个 CPU 或内核,它可能会高于实时。
标签: c multithreading time real-time systemtime