C++ 树和线程c++;

C++ 树和线程c++;,c++,multithreading,boost-thread,C++,Multithreading,Boost Thread,我有一个包含数字的树和一个布尔值,以查看数字是否已求和。这是代码: #include <iostream> #include <string> #include <boost/thread.hpp> #include <boost/lambda/bind.hpp> using namespace std; class ThreadBase { private: boost::shared_ptr<boost::thread>

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

#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;
}
#包括
#包括
#包括
#包括
使用名称空间std;
类线程库
{
私人:
boost::共享线程;
公众:
ThreadBase():m_thread_ptr(){}
虚拟~ThreadBase(){}
虚空运行()=0;
void start()
{
if(m_thread_ptr==NULL)
{
m_线程_ptr.reset(
新的boost::线程(
boost::lambda::bind(&ThreadBase::run,this));
}
其他的
{
抛出std::运行时_错误(“多次启动”);
}
}
void join()
{
如果(m_线程\u ptr)
{
m_thread_ptr->join();
}
}
};
结构节点
{
int key_值;
节点*左;
节点*对;
布尔多;
};
B类树
{
公众:
b树();
~b树();
无效插入(int键);
节点*搜索(int键);
void destroy_tree();
按顺序无效(节点*叶);
节点*根;
无效显示(节点*叶,字符串,其中);
私人:
空心树(节点*叶);
无效插入(int键,节点*叶);
节点集*搜索(int键,节点集*叶);
};
Btree::Btree()
{
cout left->left=NULL;//将左子级设置为NULL
leaf->left->right=NULL;//将右子级设置为NULL
}
}
否则如果(键>=叶->键值)
{
如果(叶->右!=NULL)
插入(键,叶->右);
其他的
{
cout key_value=key;
leaf->right->left=NULL;//将左侧节点设置为NULL
叶->右->右=NULL;//将右节点设置为NULL
}
}
}
节点集*Btree::搜索(int键,节点集*leaf)
{
左键->键值+下一步->右键->键值;
下一步->左->完成=真;
下一步->右->完成=真;
//一切都好
返回0;
}
//我们在一个父节点中,有一个节点是正确的
否则如果(下一步->左->左==NULL&&next->右->右!=NULL&&next->右->完成==false)
{
//求和值+0并将标志设置为true
下一步->键值=下一步->右->键值+0;
下一步->右->完成=真;
//一切都很好
返回0;
}
//我们位于左侧节点的父节点中
否则如果(下一步->左->左!=NULL&&next->右->右==NULL&&next->左->完成==false)
{
//求和值+0并将标志设置为true
下一步->键值=下一步->左->键值+0;
下一步->左->完成=真;
//一切都很好
返回0;
}

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

是的,我计划使用这样的监视器