Francesco Bernouli
Francesco Bernouli

Reputation: 77

C Language multiple thread addition wont work

I give 4 numbers. 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.

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

Upvotes: 2

Views: 89

Answers (3)

Alexander James Pane
Alexander James Pane

Reputation: 648

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

Upvotes: 0

dbush
dbush

Reputation: 224377

There are two problems here. First the array global can only hold 3 elements, but you attempt to set 4 elements. The 3 is the declaration specifies the size, not the index of the largest element. So set the size to 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.

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

Upvotes: 2

kiran Biradar
kiran Biradar

Reputation: 12742

change this

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

to

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

Upvotes: 1

Related Questions