Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C language question using pthread say I have the following: #import #define SIZE

ID: 3871921 • Letter: C

Question

C language question using pthread

say I have the following:

#import

#define SIZE 9

int funct1(int size, grid[SIZE][SIZE])

{

int sum = 0;

for(int i = 0; i < SIZE;i++)

{

for(int j = 0; j < SIZE;j++)

{

sum += grid[i][j]

}

}

}

int funct2(int size, grid[SIZE][SIZE])

{

int sum = 0;

for(int i = 0; i < SIZE;i++)

{

for(int j = 0; j < SIZE;j++)

{

sum += grid[i][j]

}

}

}

main()

{

pthread_t tid;

pthread_attr_t attr;

//How do I make a seperate thread for funct 1 and funct2 and get the return value for each of those returning functions?

//I also keep getting an error when I do pthread_create.

//The error says undefined reference to pthread_create

//THis is done in linux

}

Explanation / Answer

#include <stdio.h>

#include <pthread.h>

#include <stdlib.h>

#define SIZE 9

int funct1(int size, grid[SIZE][SIZE]){

int sum = 0;

for(int i = 0; i < SIZE;i++)

{

for(int j = 0; j < SIZE;j++)

{

sum += grid[i][j]

}

}

printf("value in funct1 %d",sum)

return 0;

}

int funct2(int size, grid[SIZE][SIZE]){

int sum = 0;

for(int i = 0; i < SIZE;i++)

{

for(int j = 0; j < SIZE;j++)

{

sum += grid[i][j]

}

}

printf("value in funct2 %d",sum)

return 0;

}

main()

{

pthread_t thread1,thread2;

pthread_attr_t attr;

pthread_create(&thread1, NULL, funct1, "Foo");

pthread_create(&thread2, NULL, funct2, "Bar");

pthread_join(thread1, NULL);

pthread_join(thread2, NULL);

//How do I make a seperate thread for funct 1 and funct2 and get the return value for each of those returning functions?

//I also keep getting an error when I do pthread_create.

//The error says undefined reference to pthread_create

//THis is done in linux