C - 将多线程程序写入平方数

时间:2016-02-28 01:54:26

标签: c multithreading pthreads

我试图编写一个程序,使用多个线程来计算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循环中。这会有用吗?提前谢谢。

1 个答案:

答案 0 :(得分:1)

为了避免线程争用文件访问的问题,每个线程都可以返回将由主进程放入文件的结果。或者线程可以在写入文件之前准备结果字符串和锁定。以下是第一个解决方案

#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;
}