[英]C - writing a multi-threaded program to square numbers
我正在嘗試編寫一個程序,該程序使用多個線程來計算1-10000之間的數字平方。 我試圖使每個線程一次平方一個數字,最多8個線程。 這意味着線程1-8將對8個數字求平方,並在完成時開始對下一個數字進行平方等。
我的代碼編譯沒有錯誤,但是沒有將任何內容輸出到輸出文件。 我無法確切指出問題所在,所以有人可以給我一些提示或為我指明正確的方向嗎?
另外,對於那些提供代碼幫助的人,我評論了一些內容,我不希望對其進行更改。 我仍然懷疑它們是否仍然需要,但是我將它們用於該項目的其他部分,並希望保持它們不變。 謝謝。 這是我的代碼:
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <pthread.h>
#define NUMBER_OF_THREADS 8
#define START_NUMBER 1
#define END_NUMBER 10000
FILE *f;
void *sqrtfunc(void *tid) { //function for computing squares
int i;
for (i = START_NUMBER; i<=END_NUMBER; i++){
fprintf(f, "%lu squared = %lu\n", i, i*i);
}
}
int main(){
//Do not modify starting here
struct timeval start_time, end_time;
gettimeofday(&start_time, 0);
long unsigned i;
f = fopen("./squared_numbers.txt", "w");
//Do not modify ending here
pthread_t mythreads[NUMBER_OF_THREADS]; //thread variable
long mystatus;
for (i = 0; i < NUMBER_OF_THREADS; i++){ //loop to create 8 threads
mystatus = pthread_create(&mythreads[i], NULL, sqrtfunc, (void *)i);
if (mystatus != 0){ //check if pthread_create worked
printf("pthread_create failed\n");
exit(-1);
}
}
for (i = 0; i < NUMBER_OF_THREADS; i++){
if(pthread_join(mythreads[i], NULL)){
printf("Thread failed\n");
}
}
exit(1);
//Do not modify starting here
fclose(f);
gettimeofday(&end_time, 0);
float elapsed = (end_time.tv_sec-start_time.tv_sec) * 1000.0f + \
(end_time.tv_usec-start_time.tv_usec) / 1000.0f;
printf("took %0.2f milliseconds\n", elapsed);
//Do not modify ending here
}
我能想到的唯一解決方案是移動創建8個線程的for循環並將其放置在sqrtfunc函數的for循環內。 這行得通嗎? 提前致謝。
為了避免線程爭用文件訪問的問題,每個線程都可以返回結果,該結果將由主進程放入文件中。 否則線程可以准備結果字符串並鎖定,然后僅將這個字符串寫入文件。 下面是第一個解決方案
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <pthread.h>
#define NUMBER_OF_THREADS 8
#define START_NUMBER 1
#define END_NUMBER 1000
FILE* f;
struct Thread_argument
{
unsigned long long start;
int range;
};
void* sqrtfunc( void* a ) //function for computing squares
{
struct Thread_argument* argument = ( struct Thread_argument* )a;
unsigned long long* result = calloc( argument->range, sizeof( unsigned long long ) );
for( int i = 0; i < argument->range; i++ )
{
result[i] = ( argument->start + i ) * ( argument->start + i );
}
free( a );
return result;
}
int main()
{
//Do not modify starting here
struct timeval start_time, end_time;
gettimeofday( &start_time, 0 );
long unsigned i;
f = fopen( "./squared_numbers.txt", "w" );
//Do not modify ending here
pthread_t mythreads[NUMBER_OF_THREADS]; //thread variable
long mystatus;
int END = END_NUMBER + 1;
int const range = ( END - START_NUMBER ) / ( NUMBER_OF_THREADS - 1 );
for( int i = 0; i < NUMBER_OF_THREADS; i++ ) //loop to create 8 threads
{
struct Thread_argument* ta = malloc( sizeof( struct Thread_argument ) );
ta->start = i * range + START_NUMBER;
ta->range = range;
if( i == NUMBER_OF_THREADS - 1 )
{
ta->range = ( END - START_NUMBER ) % ( NUMBER_OF_THREADS - 1 );
}
mystatus = pthread_create( &mythreads[i], NULL, sqrtfunc, ( void* )ta );
if( mystatus != 0 ) //check if pthread_create worked
{
printf( "pthread_create failed\n" );
exit( -1 );
}
}
unsigned long long* results[NUMBER_OF_THREADS]; //thread variable
for( int i = 0; i < NUMBER_OF_THREADS; i++ )
{
if( pthread_join( mythreads[i], ( void** )&results[i] ) )
{
printf( "Thread failed\n" );
}
}
for( int i = 0; i < NUMBER_OF_THREADS - 1; i++ )
{
for( int j = 0; j < range; ++j )
{
fprintf( f, "%d %lld\n", i * range + j + START_NUMBER, results[ i ][ j ] );
}
free( results[ i ] );
}
int leftovers = ( END - START_NUMBER ) % ( NUMBER_OF_THREADS - 1 );
for( int i = 0; i < leftovers; ++i )
{
fprintf( f, "%d %lld\n", ( NUMBER_OF_THREADS - 1 ) * range + i + 1, results[ NUMBER_OF_THREADS - 1 ][ i ] );
}
free( results[ NUMBER_OF_THREADS - 1 ] );
fclose( f );
//Do not modify starting here
gettimeofday( &end_time, 0 );
float elapsed = ( end_time.tv_sec - start_time.tv_sec ) * 1000.0f + \
( end_time.tv_usec - start_time.tv_usec ) / 1000.0f;
printf( "took %0.2f milliseconds\n", elapsed );
//Do not modify ending here
return 0;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.