简体   繁体   English

线程和并行编程

[英]Threads and parallel programming

Say I want to compute the product of n complex numbers.假设我想计算 n 个复数的乘积。

What I'm trying to do is that compute the product of the 2*i and 2*i+1 (i=0;i<n/2) complex numbers in threads.我想要做的是计算线程中 2*i 和 2*i+1 (i=0;i<n/2)复数的乘积。 ie, clump 2 numbers together and compute their product, therefore I shall get n/2 products.即,将 2 个数字聚集在一起并计算它们的乘积,因此我将得到 n/2 个乘积。 Then again perform the same action on these n/2 products.然后再次对这些 n/2 产品执行相同的操作。 So on and so forth, and carry on till the value of n is 1.依此类推,直到n的值为1。

Here's my code这是我的代码

#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
struct complex{
    int a;
    int b;
};
struct complex arr[1000];
struct arg {
    struct complex arr1;
    struct complex arr2;
    int i;
};
//struct arg *argv;
struct arg *argv=malloc(sizeof(struct arg));
void *multiplier(struct arg *argv)
{
    int real,imaginary;
    real=(argv->arr1.a)*(argv->arr2.a)-(argv->arr1.b)*(argv->arr2.b);
    imaginary=(argv->arr1.a)*(argv->arr2.b)+(argv->arr1.b)*(argv->arr2.a);
    arr[argv->i].a=real;
    arr[argv->i].b=imaginary;
    printf("real=%d imaginary=%d no=%d\n",real,imaginary,argv->i);
    pthread_exit(0);
}
int main(void)
{
    int n,i,j,flag=0,q;
    pthread_t tid;
    pthread_attr_t attr;
    pthread_attr_init(&attr);
    scanf("%d",&n);
    for(i=0;i<n;i++)
        scanf("%d + i%d",&arr[i].a,&arr[i].b);
    for(i=0;i<n;i++)
        printf("%d + i%d\n",arr[i].a,arr[i].b);
    while(n!=0)
    {
        if(n%2==1)
            flag=1;
        else
            flag=0;
        for(i=0;i<n/2;i++)
        {
            argv->arr1.a=arr[2*i].a; /* SEG FAULT HERE */
            argv->arr1.a=arr[2*i].b;
            argv->arr2.a=arr[2*i+1].a;
            argv->arr2.a=arr[2*i+1].b;
            argv->i=i;
            pthread_create(&tid,&attr,multiplier,(void *) argv);
        }
        pthread_join(tid,NULL);
        if(flag==1)
        {
            arr[n/2].a=arr[n-1].a;
            arr[n/2].b=arr[n-1].b;
        }
        n=n/2;
    }
    return(0);
}

However my code gives me seg fault at line 45. I've been trying to figure out what's wrong with it but to no avail.然而,我的代码在第 45 行给了我段错误。我一直试图找出它有什么问题,但无济于事。 I probably might be making a fundamentally horrendous error, but do help me out.我可能犯了一个根本可怕的错误,但请帮助我。

EDIT 1: Probably the most stupid error ever.编辑 1:可能是有史以来最愚蠢的错误。 I can't allocate memory globally like the way I just did.我不能像刚才那样全局分配内存。

I just inserted the Malloc into the main function and the program works.我刚刚将 Malloc 插入到 main 函数中,程序就可以运行了。

Each thread needs it's own memory passed in via argv to not overwrite the other thread data.每个线程都需要通过argv传入自己的内存,以免覆盖其他线程数据。

So you might liek to move this line所以你可能会谎称移动这条线

struct arg * argv = malloc(sizeof(struct arg));

to here:到这里:

for(i = 0; i < n/2; ++i)
    {
        struct arg * argv = malloc(sizeof(*argv));
        argv->arr1.a = arr[2*i].a; 

Also checking the result of the calls malloc() might not be a bad idea.另外检查调用malloc()的结果可能不是一个坏主意。


Then let the thread function free() its memory when its done with it:然后让线程函数free()它的内存:

void * multiplier(struct arg * argv)
{
    ...

    free(argv);

    pthread_exit(0);
}

Also the thread function to be passed to pthread_create() is defined as:还要传递给pthread_create()的线程函数定义为:

void *(*)(void *)

So you shall declare yours this way:所以你应该这样声明你的:

void * multiplier(void * pvargv)
{
    struct arg * argv = pvargv;
    ...

It's pretty hard to figure out which of your lines is line 45.很难弄清楚你的哪一行是第 45 行。

Also, this looks very wrong:此外,这看起来非常错误:

struct arg *argv=malloc(5*sizeof(struct complex));

It's very rarely correct to mis-match types like that, and struct complex looks nothing like struct arg , so this really seems strange.像这样的错误匹配类型很少是正确的,并且struct complex看起来与struct arg ,所以这看起来真的很奇怪。 Also, you shouldn't have a global named argv while u此外,你不应该有一个全局命名的argv而你

First, I'm not sure if the memory size you allocate for argv is sane enough.首先,我不确定您为argv分配的内存大小是否足够合理。 Second, you modify this argv thing, create a thread and immediately overwrite it, probably, before the thread even gets its hands on it.其次,你修改这个argv东西,创建一个线程并立即覆盖它,可能是在线程得到它之前。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM