计算的款项使用多线程 [英] Computing Sums Using Multithreading

查看:110
本文介绍了计算的款项使用多线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个程序,是采取从一个文件15000整数输入。读取数据后,线程应该再创建10个线程与负责计算自己的块的总和每个线程(每个1500值)。然后,每个线程将打印其值的总和,而主线程将计算所有10个线程的总和。

I am working on a program that is to take the input of 15,000 integers from a file. After reading the values, the thread should then create 10 threads with each thread responsible for computing the sum of their block (1,500 values each). Each thread will then print the sum of its values, and the main thread will compute the sum from all 10 threads.

我有的是头脑读取所有值,并同时使用一个int保留值的数量计数并将其存储在一个int数组中读取(我们称之为诠释值)。然后我会除以线程我想确定每块的每个线程应该有值的数目的数目,此数字(我们称之为INT块)。那么我会启动一个线程,循环遍历数组(INT块倍),而递增数组索引数,然后只要数组的索引数不等于最后的数组索引开始一个新的线程。

What I have is mind is to read in all values and store them in an int array while using an int to keep count of the number of values read (let's call it int values). I would then divide this number by the number of threads I would like to determine the number of values per block that each thread should have (let's call it int block). I would then start a thread, loop through the array (int block times) while incrementing the array index count, and then start a new thread as long as the array index count does not equal the last array index.

这是看待这个问题的正确方法?有没有更简单的方法呢?我们得到了提示,利用pthread_create的,在pthread_join,了pthread_exit,pthread_attr_init,pthread_attr_destroy和pthread_setdetachstate。这是我在多线程的第一次尝试,所以这将是巨大的,得到凡在我code,我应该发起和结束每线程,以便它实际上是多线程的,而不是执行单个线程多次反馈。任何帮助将大大AP preciated!

Is this the right way of looking at this problem? Is there a simpler approach? We have been given hints to utilize pthread_create, pthread_join, pthread_exit, pthread_attr_init, pthread_attr_destroy, and pthread_setdetachstate. This is my first attempt at multithreading, so it would be great to get feedback on where in my code I should initiate and end each thread so that it is actually multithreading and not performing an individual thread multiple times. Any help would be greatly appreciated!

编辑:卡住的命令行参数

#include <stdio.h>

int main(int argc, char *argv[]) {
int i;
FILE *fp;
int c;

for (i = 1; i < argc; i++) {
    fp = fopen(argv[i], "r");

    if (fp == NULL) {
        fprint(stderr, "cat: can't open %s\n", argv[i]);
        continue;
    }

    while ((c = getc(fp)) != EOF) {
        putchar(c);
    }

    fclose(fp);
}

return 0;
}

我似乎已经忘记了我是多么可怕的是在I / O时,我们讨论了这一节。什么是命令行参数与给定的参数来测试我的程序(prob_5.c)?

I seem to have forgotten how terrible I was at I/O when we covered this section. What are the command line arguments to test my program (prob_5.c) with a given parameter?

推荐答案

您的计划听起来不错。如果我是你,我会尝试执行,并带回来的具体问题,你应该遇到什么。

Your plan sounds good. If I were you, I'd try to execute it and come back with specific problems should you encounter any.

这将是巨大的,得到我的code我应该在哪里反馈
  发起和结束每线程

it would be great to get feedback on where in my code I should initiate and end each thread

您的主线程将创建工人。这将是您的一些细节描述了回路的一部分。在所有的可能性,工人会从他们的线程函数返回终止。

Your main thread will be creating the workers. This would be part of the loop that you describe in some detail. In all likelihood the workers will terminate by returning from their thread function.

一件事,你不应该期待是加速。在所有的概率你十个工人的版本会比它的单线程相当于慢。这具有与输入阵列的小尺寸和产卵线程和后续同步的开销做。此外,由于在正确的评论所指出的@Adam罗森菲尔德,整体方案很可能是I /反正O限制。

One thing that you shouldn't be expecting is speed-up. In all probability your ten-worker version will be slower than its single-threaded equivalent. This has to do with the small size of the input array and the overhead of spawning threads and subsequent synchronization. Besides, as rightly pointed out by @Adam Rosenfield in the comments, the overall program is likely to be I/O-bound anyway.

这篇关于计算的款项使用多线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆