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?
The system time is the amount of time your program spends in the operating system; as opposed to user time, which is the time your program spends running your direct code ( + libraries ).
You seem to have discovered that creating and deleting threads takes a lot of system time. You may also have discovered that incrementing a number 10000 times takes a lot less time than creating and deleting threads. These are good lessons.
Also, depending upon whether your mutex is properly initialized; that it takes very little time to execute mutex operations on an invalid mutex; or that you are spending a lot of time having 1000 threads thrashing on a single mutex. To figure out which, you could check the return values from these functions; often illuminating.
Maybe start with a smaller number of threads. For a program like yours, there is nothing to be gained by having more threads than CPUs. That isn't true of all programs; some threads periodically wait for I/O operations and the like; but there is seldom much justification for 1000s of threads.