Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用pthreads的完美数计算器_Pthreads_Perfect Numbers - Fatal编程技术网

使用pthreads的完美数计算器

使用pthreads的完美数计算器,pthreads,perfect-numbers,Pthreads,Perfect Numbers,我正在尝试制作一个程序,它使用一个非常低效的算法,使用POSIX线程计算一定范围内的完美数字。我似乎不能很好地理解锁定的概念,以使我的算法正常工作。我想返回一个完美数字列表。有人能就如何更好地实施这一点提供一些建议吗 具体问题: -如何使它只打印出每个完美数字的1个瞬间? -如何使其返回值而不是仅打印值 资料来源: static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER; static void * threadFunc(void *arg

我正在尝试制作一个程序,它使用一个非常低效的算法,使用POSIX线程计算一定范围内的完美数字。我似乎不能很好地理解锁定的概念,以使我的算法正常工作。我想返回一个完美数字列表。有人能就如何更好地实施这一点提供一些建议吗

具体问题: -如何使它只打印出每个完美数字的1个瞬间? -如何使其返回值而不是仅打印值

资料来源:

static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;

static void * threadFunc(void *arg) {

    int range, start, end;
    int i, s,  number, mod, divisor, temp, sum;

    s = pthread_mutex_lock(&mtx);   

    /* Takes in a string and pulls out the two integers */
    sscanf(arg,"%i-%i", &start, &end);
    printf("\nStart: %i\nEnd: %i\n", start, end);

    printf("\n");

    s = pthread_mutex_unlock(&mtx);

    for (number=start; number<=end; number++) { // loop through range of numbers                

        temp=0,sum=0;           
        // loops through divisors of a number and sums up whole divisors
        for (i=1; i<number; i++) {          
            //s = pthread_mutex_lock(&mtx);             
            mod = number % i;           
            //s = pthread_mutex_unlock(&mtx);           

            if (mod == 0){              
                s = pthread_mutex_lock(&mtx);               

                divisor = i; 
                sum = divisor + temp;
                temp = sum;

                s = pthread_mutex_unlock(&mtx);                     
            }                       
        }
        //if the sum of whole divisors is equal to the number, its perfect
        if (sum == number)  {           

            s = pthread_mutex_lock(&mtx);           

            printf("%i is a Perfect Number \n", sum);
            //return sum somehow;           

            s = pthread_mutex_unlock(&mtx);         
        }
    }

    return NULL;
}


int main(int argc, char *argv[]) {
    pthread_t tid[5];

    int prefect_number, i, s;

    char input[]="1-9999";

    for(i=0; i < 5; ++i) {
        pthread_create(&tid[i], NULL, &threadFunc, input);
        print_thread_info();
    }
    /* Wait for the perfect number thread to complete, then get result. */  
    for(i=0; i < 5; ++i)
        pthread_join(tid[i],NULL);

    return 0;   
}
静态pthread\u mutex\u t mtx=pthread\u mutex\u初始值设定项;
静态void*threadFunc(void*arg){
整数范围,开始,结束;
整数i,s,数,模,除数,温度,和;
s=pthread\u mutex\u lock(&mtx);
/*接收一个字符串并提取两个整数*/
sscanf(参数、%i-%i、&start和end);
printf(“\n开始:%i\n开始:%i\n”,开始,结束);
printf(“\n”);
s=pthread\u mutex\u unlock(&mtx);

对于(number=start;number,在访问可能从另一个线程修改的数据结构(例如,全局变量)或资源(例如,终端)时,只需锁定互斥锁

  • 访问单个线程的本地变量不需要锁定互斥锁
  • 您不需要锁定互斥锁来读取从未被其他线程修改过的全局变量
因此,在您的例子中,互斥体的单一使用情形是防止多个printf()的输出可能混淆

另外,让所有线程都探索相同的范围是没有意义的。也许你想要5个线程分别探索不同的范围(0-1999、2000-2999…8000-9999)

pthread_join()返回线程的退出值(传递给return或pthread_exit()的值)。如果要返回多个值,可能需要创建一个包含数字链接列表的全局变量(最后需要一个互斥体,因为不同的线程需要对其进行写入)