有关函数pthread_join()&c语言返回值的问题
我正在使用pthread_join()以C语言获取返回值,其第二个参数不是null,而是2级指针。因为我想获得一个矩阵。
但是,每次我在返回线程后调试以检查矩阵,并且其内部的数据完全错误时,问题是线程函数块中的结果是正确的,这可能意味着错误发生在期间。返回的过程。
#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;
}
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论