简体   繁体   English

C语言多线程添加将不起作用

[英]C Language multiple thread addition wont work

I give 4 numbers. 我给4个数字。 I want to sum the first two and then the next 2 in different threads. 我想对不同线程中的前两个和后两个求和。 I created a program that can add the first two numbers correctly, but not the next 2. I just began to learn how pthreads work so any help would be appreciated. 我创建了一个程序,该程序可以正确添加前两个数字,但不能正确添加后两个数字。我刚刚开始学习pthreads的工作原理,因此将不胜感激。

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
int global[3];

void *sum_thread(void *arg)
{
    int *args_array;
    args_array = arg;
    int n1,n2,sum;
    n1=args_array[0];
    n2=args_array[1];
    sum = n1+n2;

    printf("N1 + N2 = %d\n",sum);

    return sum;
}
void *sum_thread1(void *arg)
{
    int *args_array;
    args_array = arg;
    int n3,n4,sum2;
    n3=args_array[0];
    n4=args_array[1];
    sum2=n3+n4;

    printf("N3 + N4 = %d\n",sum2);

    return sum2;
}



int main()
{
    printf("First number: ");
    scanf("%d",&global[0]);

    printf("Second number: ");
    scanf("%d",&global[1]);

    printf("Third number: ");
    scanf("%d",&global[2]);

    printf("Fourth number: ");
    scanf("%d",&global[3]);

    pthread_t tid_sum;
    pthread_create(&tid_sum,NULL,sum_thread,global);
    pthread_join(tid_sum,NULL);

    pthread_t tid_sum1;
    pthread_create(&tid_sum1,NULL,sum_thread1,global);
    pthread_join(tid_sum1,NULL);

    return 0;
}

There are two problems here. 这里有两个问题。 First the array global can only hold 3 elements, but you attempt to set 4 elements. 首先, global数组只能容纳3个元素,但是您尝试设置4个元素。 The 3 is the declaration specifies the size , not the index of the largest element. 3是声明指定的大小 ,而不是最大元素的索引。 So set the size to 4: 因此,将大小设置为4:

int global[4];

Second, both thread functions are adding the same elements. 其次,两个线程函数都添加相同的元素。 You need to have one of them get array indexes 0 and 1, and the other get array indexes 2 and 3. 您需要让其中一个获取数组索引0和1,另一个获取数组索引2和3。

n3=args_array[2];
n4=args_array[3];

change this 改变这个

n3=args_array[0];
n4=args_array[1];

to

n3=args_array[2];
n4=args_array[3];

You just wrote the wrong indexes in the second thread, this should work: 您在第二个线程中编写了错误的索引,这应该可以工作:

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
int global[4];

void *sum_thread(void *arg)
{
    int *args_array;
    args_array = arg;
    int n1,n2,sum;
    n1=args_array[0];
    n2=args_array[1];
    sum = n1+n2;

    printf("N1 + N2 = %d\n",sum);

    return sum;
}
void *sum_thread1(void *arg)
{
    int *args_array;
    args_array = arg;
    int n3,n4,sum2;
    //this is args_array[2] and not args_array[0]
    n3=args_array[2];
    //this is args_array[3] and not args_array[1]
    n4=args_array[3];
    sum2=n3+n4;

    printf("N3 + N4 = %d\n",sum2);

    return sum2;
}



int main()
{
    printf("First number: ");
    scanf("%d",&global[0]);

    printf("Second number: ");
    scanf("%d",&global[1]);

    printf("Third number: ");
    scanf("%d",&global[2]);

    printf("Fourth number: ");
    scanf("%d",&global[3]);

    pthread_t tid_sum;
    pthread_create(&tid_sum,NULL,sum_thread,global);
    pthread_join(tid_sum,NULL);

    pthread_t tid_sum1;
    pthread_create(&tid_sum1,NULL,sum_thread1,global);
    pthread_join(tid_sum1,NULL);

    return 0;
}

Since the two threads practically perform the same operations, you can just declare one thread routine and pass the correct pointers: 由于两个线程实际上执行相同的操作,因此您只需声明一个线程例程并传递正确的指针即可:

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
int global[4];

void *sum_thread(void *arg)
{
    int *args_array;
    args_array = arg;
    int n1,n2,sum;
    n1=args_array[0];
    n2=args_array[1];
    sum = n1+n2;

    printf("N1 + N2 = %d\n",sum);

    return sum;
}

int main()
{
    printf("First number: ");
    scanf("%d",&global[0]);

    printf("Second number: ");
    scanf("%d",&global[1]);

    printf("Third number: ");
    scanf("%d",&global[2]);

    printf("Fourth number: ");
    scanf("%d",&global[3]);

    pthread_t tid_sum, tid_sum1;
    pthread_create(&tid_sum,NULL,sum_thread,global);
    pthread_create(&tid_sum1,NULL,sum_thread,global + 2);
    pthread_join(tid_sum,NULL);
    pthread_join(tid_sum1,NULL);

    return 0;
}

also the global variable must be able to contain 4 elements and not 3 而且全局变量必须能够包含4个元素,而不是3个

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

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