Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8439237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T07:57:19+00:00 2026-06-10T07:57:19+00:00

I have a tree data structure. In addition to the normal insert and removal

  • 0

I have a tree data structure. In addition to the normal insert and removal functions I have a COMPUTE function which computes certain values from the nodes present in the tree. The insert and removal functions affect the tree. But the COMPUTE function does not modify the tree. The COMPUTE function has its own internal queues and adds and deletes nodes to these queues. Here I present a simplified pseudocode of the compute function:

class tree{
    vector<int> value;
    tree * children
}

tree::COMPUTE(vector<int> inputValue)
{
    Set currentSet;
    Set nextSet
    currentSet.add(root);

    foreach (node in currentSet)
    {
        if(node has children)
            foreach(childNode of node)
            {
                if(CONDITION_SATISFIED(childNode))
                    add childNode to nextSet;
            }
        else
            output childNode
    }


}

So the COMPUTE function simply recurses through the tree, adds nodes to its own internal set datastructure and does some computations.

Now I want to multithread the COMPUTE function. Since it does not modify the tree, I guess this is easily doable. Each thread gives its own value and gets its own COMPUTE output. The code is written in C++.

My question is this:

If I call it like this:

void * threadRoutine
{
    tree.COMPUTE(thisThreadVal);
}

int main()
{
    tree myTree();

    //Insertion Code here

    call N threads each with different parameters but the same tree.
}

I dont this it is correct. This is because all the threads will call the same member function of the same tree object. So the internal data strcutures like the sets used in the COMPUTE method will be mangled.

I think I should write the compute function outside the tree and not as a member function of the tree. Can somebody tell me if this is right.

btw: In case somebody is curious, the exact tree that I will be using is called a Cover Tree. It is a relatively new data structure used for finding nearest neighbour queries.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T07:57:20+00:00Added an answer on June 10, 2026 at 7:57 am

    The approach taken will be safe for multiple COMPUTE (what’s with the all-caps?) calls as long as the sets used are locals or else in the heap but the local pointer or reference to them is the only such pointer or reference – they will therefore stay out of each others’ way.

    (It can be thread-safe in other cases, but it gets a lot more complicated proving it).

    It will not be safe to do this while another thread is doing the work that adds or deletes; computes are safe to happen along with other computes, but not along with modifications. If that can happen you need some sort of synchronisation, or to consider all the possible operations together and make them all thread-safe.

    Whether the function is a member function or outside is completely irrelevant.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a speed critical multithreaded program which involves data in a tree structure.
I have the following function that prunes a tree data structure : public static
I have a large tree-like data structure of objects which behave mostly identical but
I have a MySQL table with a tree data structure. The fields are _id
I have a binary tree function with 3 pieces of data in each node.
I have to build a tree using the following data which comes from a
I am looking for a good Tree data structure class. I have come across
Let's say I have a binary tree data structure defined as follows type 'a
I have a pretty large tree data structure that will get updated (nodes removed
Which Tree data structure in Java allows querying for different levels of children? I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.