It is a program that calculates the number of divisors of 10000 as the maximum number of threads created by 1000.
I thought realtime was larger and turned.
n_Threads is 1000
thread.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;
}
result
10000 number of proper divisor : 25
Real time : 0.40000 sec
User time : 0.09000 sec
System time : 0.59000 sec
why system time larger than real time?