Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C线程创建树_C_Multithreading - Fatal编程技术网

C线程创建树

C线程创建树,c,multithreading,C,Multithreading,我试图生成一个线程树,其中每个线程再创建两个线程,以此类推。到达树端(命令行arg),我需要向后打印分支 我放弃了使用malloc和类似工具,因为我在错误中迷失了方向,现在我正在使用固定大小的阵列。但我仍然会犯错误,甚至使用valgrind也没有真正的帮助。 理论上,我应该只能使用pthread_create(没有属性)来实现这一点,但是我感到非常困惑,你能帮我找出内存泄漏发生在哪里吗? Valgrind的结果主要包括“从tS复制”行,但我不知道问题出在哪里 我真的不是专家,所以我不排除会犯一些

我试图生成一个线程树,其中每个线程再创建两个线程,以此类推。到达树端(命令行arg),我需要向后打印分支

我放弃了使用malloc和类似工具,因为我在错误中迷失了方向,现在我正在使用固定大小的阵列。但我仍然会犯错误,甚至使用valgrind也没有真正的帮助。 理论上,我应该只能使用pthread_create(没有属性)来实现这一点,但是我感到非常困惑,你能帮我找出内存泄漏发生在哪里吗? Valgrind的结果主要包括“从tS复制”行,但我不知道问题出在哪里

我真的不是专家,所以我不排除会犯一些愚蠢的错误,谢谢你的耐心

我附上密码

#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <stdlib.h>
#include <math.h>

int maxDepth;

typedef struct {
    int d;
    pthread_t *b;
} tS;

void *tF (void *svp) {
    tS *sp, s, t[2];
    int d, i;
    pthread_t branch[maxDepth], mythread;

    /*get the struct*/
    sp = (tS *) svp;
    s = *sp;

    /*copy tS values*/
    d=s.d;
    for (i =0; i< d; i++) {
        branch[i]=s.b[i];
    }

    /*iterate or print*/
    if (d < maxDepth) {
        for (i=0; i<2; i++) {
            t[i].d = d+1;
            t[i].b = branch;
            t[i].b[d] = pthread_self();
            pthread_create(&mythread, NULL, tF, (void *) &t[i]);
        }
    } else {
        printf("Thread tree: ");
        for (i =0; i< maxDepth; i++) {
            printf("%ld ", branch[i]);
        }
        putchar('\n');
    }

    pthread_exit(NULL);
}

int main(int argc, char **argv) {
    maxDepth = atoi(argv[1]);

    int i;
    pthread_t branch[maxDepth];
    pthread_t mythread;
    tS t[2];

    for (i=0; i<2; i++) {
        t[i].d = 1;
        t[i].b= branch;
        t[i].b[0] = pthread_self(); 
        pthread_create(&mythread, NULL, tF, (void *) &t[i]);
    }
    pthread_exit(NULL);
    return 0;

}
#包括
#包括
#包括
#包括
#包括
int最大深度;
类型定义结构{
int d;
pthread_t*b;
}tS;
void*tF(void*svp){
tS*sp,s,t[2];
int d,i;
pthread_t分支[maxDepth],mythread;
/*获取结构*/
sp=(tS*)高级副总裁;
s=*sp;
/*复制tS值*/
d=标准差;
对于(i=0;i
tS t[2];
pthread_t  branch[maxDepth];
是局部变量,在控件退出函数后将被销毁,因为您没有
join
位置

并且将与参数相同的变量传递给
pthread\u create()


嘿,谢谢。我没有使用join,因为(作为一种任务)任务没有要求它。现在我尝试添加它,并且SEGFULT确实消失了。只是一个问题,如果我使用malloc选项,我在哪里可以释放内存而不把一切都搞砸?
  for (i=0; i<2; i++) {
            t[i].d = d+1;
            t[i].b = branch;  // Here
            t[i].b[d] = pthread_self();
            pthread_create(&mythread, NULL, tF, (void *) &t[i]); //and here
        }
   tS *t = malloc(sizoeof(*t)*2);
   t[i].b = malloc(sizeof(pthread_t)*maxDepth);