简体   繁体   English

并发BST插入功能不起作用

[英]Concurrent BST insert function not working

I am trying to implement a concurrent BST, however the code isn't working exactly as its supposed to. 我正在尝试实现并发BST,但是代码无法正常运行。 I am writng the parts here which is causing the problem. 我在这里写引起问题的零件。

void* insertfunc(void* arg)
{
    int r, i;
    for(i = 0; i < size_of_tree; i++)
    {
      r = rand();
      root = insert(root,r); //insert function is correct, it was tested on a serial BST
     }
    inorder(root); //here tree is printed properly
    printf("\n");
   return;
}

int main()
{
       char c;
        srand(time(NULL)); 
        int i;
        node* root = NULL;
        pthread_t mythreads[4];

        for(i=0; i<1; i++)
       {
             pthread_create(&mythreads[i],NULL, insertfunc,NULL);
       }

       for(i=0; i<1; i++)
       {
              pthread_join(mythreads[i],NULL);
       }
        printf("%d", root -> data); //here root is NULL and hence the segfault
}
    return 0;
}

My root is a global variable. 我的根是全局变量。 I can't figure out why, although in my thread the value of root is changing as a node is being inserted, when the thread terminates and my main thread resumes to complete execution, why is the root NULL again? 我不知道为什么,尽管在我的线程中,随着插入节点而改变了root的值,但是当线程终止并且我的主线程恢复完成执行时,为什么再次将NULL设为NULL?

There is a local 有一个地方

node* root = NULL;

inside main() so it is always NULL. main()内部,因此始终为NULL。 If you have root as global then remove this as we see this will be always NULL. 如果您将root为全局,则将其删除,因为我们将看到它始终为NULL。 So segmentation fault. 所以分割错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM