树和线程 C++

tree sum threaded c++

本文关键字:C++ 线程      更新时间:2023-10-16

我有一个带有数字和一个布尔值的树,以查看数字是否已求和,这是代码:

#include <iostream>
#include <string>
#include <boost/thread.hpp>
#include <boost/lambda/bind.hpp>
using namespace std;
class ThreadBase
{
private:
    boost::shared_ptr<boost::thread> m_thread_ptr;
public:
    ThreadBase() : m_thread_ptr() { }
    virtual ~ThreadBase() { }
    virtual void run() = 0;
    void start()
    {
        if (m_thread_ptr == NULL)
        {
            m_thread_ptr.reset(
                new boost::thread(
                    boost::lambda::bind(&ThreadBase::run, this)));
        }
        else
        {
            throw std::runtime_error("multiple start");
        }
    }
    void join()
    {
        if (m_thread_ptr)
        {
            m_thread_ptr->join();
        }
    }
};
struct NodeT
{
    int key_value;
    NodeT *left;
    NodeT *right;
    bool done;
};
class Btree
{
public:
    Btree();
    ~Btree();
    void insert(int key);
    NodeT *search(int key);
    void destroy_tree();
    void inOrder(NodeT *leaf);
    NodeT *root;
    void display(NodeT *leaf, string where);
private:
    void destroy_tree(NodeT *leaf);
    void insert(int key, NodeT *leaf);
    NodeT *search(int key, NodeT *leaf);
};
Btree::Btree()
{
    cout << "creating tree" << endl;
    root=NULL;
}
Btree::~Btree()
{
    cout << "deleting tree end" << endl;
    destroy_tree() ;
}
void Btree::destroy_tree(NodeT *leaf)
{
    if (leaf != NULL)
    {
        destroy_tree(leaf->left);
        destroy_tree(leaf->right);
        delete leaf;
        cout << "tree deleted " << leaf->key_value << endl;
    }
}
void Btree::insert(int key, NodeT *leaf)
{
    if (key < leaf->key_value)
    {
        if (leaf->left != NULL)
            insert(key, leaf->left);
        else
        {
            cout << "creating new node to insert with less value" << key << endl;
            leaf->left = new NodeT;
            leaf->left->key_value=key;
            leaf->left->left=NULL;   //sets the left child to null
            leaf->left->right=NULL;  //sets the right child to null
        }
    }
    else if (key >=leaf->key_value)
    {
        if (leaf->right != NULL)
            insert(key, leaf->right);
        else
        {
            cout << "creating node with more or equal value" << key << endl;
            leaf->right = new NodeT;
            leaf->right->key_value=key;
            leaf->right->left=NULL; //sets the left node to null
            leaf->right->right=NULL; //sets the right node to nulll
        }
    }
}
NodeT *Btree::search(int key, NodeT *leaf)
{
    cout << "searching node" << key << endl;
    if (leaf != NULL)
    {
        if(key == leaf->key_value)
        {
            cout << "finded in first attempt " << endl;
            return leaf;
        }
        if(key < leaf->key_value)
        {
            cout << "looking again with less value to the left" << endl;
            return search(key, leaf->left);
        }
        else
        {
            cout << "looking again more value to the right" << endl;
            return search(key, leaf->right);
        }
    }
    else
    {
        cout << "not found" << endl;
        return NULL;
    }
}
void Btree::insert(int key)
{
    if (root != NULL)
        insert(key, root);
    else
    {
        cout << "inserting new node private func" << key << endl;
        root=new NodeT;
        root->key_value=key;
        root->right=NULL;
        root->left=NULL;
    }
}
NodeT *Btree::search(int key)
{
    cout << "searhing node private func" << endl;
    return search(key, root);
}
void Btree::destroy_tree()
{
    cout << "deleting tree public" << endl;
    destroy_tree(root);
}
void Btree::inOrder(NodeT *leaf)
{
    if(leaf!= NULL)
    {
        inOrder(leaf->left);
        cout << "search inorder" << leaf->key_value << endl;
        inOrder(leaf->right);
    }
}
void Btree::display(NodeT *leaf, string where)
{
    cout << where << endl;
    if (leaf != NULL)
    {
        cout << leaf->key_value << " t or f"<< leaf->done << endl;
        display(leaf->left, "left");
        display(leaf->right, "rigth");
    }
}
class MyThread : public ThreadBase
{
public:
    void run()
    {
    }
    int sum( Btree *ptrRoot)
    {
        //pointer to next node or to parent
        NodeT *next = ptrRoot->root;
        //if we are in some root we should be here so leave
        if (next->left == NULL && next->right == NULL)
        {
            return 0;
        }
        //we are in a parent with 2 nodes
        else if(next->left->left != NULL && next->right->right != NULL && next->left->done == false && next->right->done == false)
        {
            //sum values and put it into this node and set bool to true
            //to not go again
            next->key_value = next->left->key_value + next->right->key_value;
            next->left->done = true;
            next->right->done = true;
            //all good
            return 0;
        }
        //we are in a parent with 1 node the right one
        else if(next->left->left == NULL && next->right->right != NULL && next->right->done == false)
        {
            //sum value + 0 and set flag to true
            next->key_value = next->right->key_value + 0;
            next->right->done = true;
            //all is good
            return 0;
        }
        //we are in a parent with the left node
        else if (next->left->left != NULL && next->right->right == NULL && next->left->done == false)
        {
            //sum value + 0 and set flag to true
            next->key_value = next->left->key_value + 0;
            next->left->done = true;
            //all is good
            return 0;
        }
        cout << "we are at here " << next->key_value << endl;
        return 0;
    }
};
int THREADS_HOW_MANY = 0;
int main()
{
    cout << "Trees example" << endl;
    cout << "make yoir choice" << endl;
    cout << "1 insert 2 delete 3 search" << endl;
    Btree bt;
    bt.insert(10);
    bt.insert(15);
    bt.insert(4);
    bt.insert(2);
    bt.insert(8);
    bt.search(4);
    bt.search(2);
    bt.search(20);
    bt.inOrder(bt.root);
    bt.display(bt.root, "root");
    cout << "end everyrhing" << endl;
    MyThread mt;
    mt.start();
    mt.run();
    mt.sum(&bt);
    THREADS_HOW_MANY = boost::thread::hardware_concurrency();
    std::cout << THREADS_HOW_MANY << std::endl;
    return 0;
}

我想做的是去所有的树上,看看有假的叶子,然后在父项中求和,再与父项等求和,直到根,这必须被线程化,所以 1 个线程对前 2 个线程求和,另一个线程对下一个线程求和无限,现在我认为我要说的方式有点不对劲,因为函数总和还没有完成, 所以你能给一些提示。

您将从不同的线程到达同一节点,因此当您访问node.done或使用"无锁"技术时,您需要使用锁定或使用"无锁"技术,例如node.done成员的比较和交换。在对节点求和之前,需要修改代码以更改"done"的值。