有关函数pthread_join()&c语言返回值的问题

发布于 2025-01-25 17:57:28 字数 4554 浏览 2 评论 0原文

我正在使用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;
  }

来自24-34是线程函数 来自146-152是问题

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

from24-34 is the thread function
from146-152 is the problem

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文