'A question about the function pthread_join()'s returning values of C language
I'm using pthread_join()to get returning value in C language,whose second argument is not NULL but a 2-level pointer. Because I want to get a matrix.
However every time I'm debugging to check the matrix after the threads returned,and the data inside of it is completely wrong, the thing is that the result in the block of the thread's function is right, which might means that the errors occurs during the process of the returning.
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
#include<time.h>
int cti(char* c)
{
const char* ptr = c;
int priceNum = 0;
while (*ptr)
{
priceNum *= 10;
priceNum += *ptr - '0';
ptr++;
}
return priceNum;
}
struct Mat
{
int a[2][10];
int b[10][10];
int c;
};
void* Mul(void* para)
{
struct Mat* p = (struct Mat*)para;
static int t[10][10] = { 0 };
int i, j, k;
for (i = 0; i < 2; i++)
for (j = 0; j < 10; j++)
for (k = 0; k < 10; k++)
t[p->c + i][j] += (p->a[i][k]) * (p->b[k][j]);
return (void*)t;
}
int main(int argc, char* argv[])
{
clock_t start, stop;
start = clock();
FILE* fpa, * fpb, * fpc;
int count = 0, n = 0, i, j, k;
char ch, c[3] = { 0 };
fpa = fopen(argv[1], "r");
fpb = fopen(argv[2], "r");
fpc = fopen("t10.txt", "w");
while (!feof(fpa))
{
ch = fgetc(fpa);
if (ch == '#')
{
fseek(fpa, 15, SEEK_CUR);
count++;
}
if (count == 2)break;
if (ch == '\n')n++;
}
rewind(fpa);
int** Ma = (int**)malloc(sizeof(int*) * n);
for (i = 0; i < n; i++)
Ma[i] = (int*)malloc(sizeof(int*) * n);
count = 0;
for (i = 0; i < n; i++)
for (j = 0; j < n;)
{
ch = fgetc(fpa);
if (ch == '#')
{
fseek(fpa, 15, SEEK_CUR);
ch = fgetc(fpa);
}
if (ch != '\n')
{
c[count] = ch;
count++;
}
else
{
c[count] = '\0';
Ma[i][j] = cti(c);
j++;
count = 0;
}
}
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
{
printf("%d\t", Ma[i][j]);
if (j == n - 1)printf("\n");
}
int** Mb = (int**)malloc(sizeof(int*) * n);
for (i = 0; i < n; i++)
Mb[i] = (int*)malloc(sizeof(int*) * n);
for (i = 0; i < n; i++)
for (j = 0; j < n;)
{
ch = fgetc(fpb);
if (ch == '#')
{
fseek(fpb, 15, SEEK_CUR);
ch = fgetc(fpb);
}
if (ch != '\n')
{
c[count] = ch;
count++;
}
else
{
c[count] = '\0';
Mb[i][j] = cti(c);
j++;
count = 0;
}
}
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
{
printf("%d\t", Mb[i][j]);
if (j == n - 1)printf("\n");
}
int** Mc = (int**)malloc(sizeof(int*) * n);
for (i = 0; i < n; i++)
Mc[i] = (int*)malloc(sizeof(int*) * n);
pthread_t p[5];
struct Mat m[5];
for (k = 0; k < 5; k++)
{
m[k].c = k * 2;
for (j = 0; j < n; j++)
{
m[k].a[0][j] = Ma[2 * k][j];
m[k].a[1][j] = Ma[2 * k + 1][j];
}
}
for (k = 0; k < 5; k++)
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
m[k].b[i][j] = Mb[i][j];
for (k = 0; k < 5; k++)
{
pthread_create(&p[k], NULL, &Mul, &m[k]);
}
int r[n][n];
for (k = 0; k < 5; k++)
{
pthread_join(p[k], (void*)&r);
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
Mc[i][j] = r[i][j];
}
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
//printf("\t%d",mat[i][j]);
if (j == 0)fprintf(fpc, "#This is Row %d:\n", i);
fprintf(fpc, "%d\n", Mc[i][j]);
//if(j==N-1)fprintf(fpa,"\n");
}
}
fclose(fpa);
fclose(fpb);
fclose(fpc);
stop = clock();
double duration = ((double)(stop - start)) / CLOCKS_PER_SEC;
printf("Time used = %.2lf\n", duration);
return 0;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
