0

こんにちは私はこのコードを持っています

class Triplets
{
public:
int nVal1;
int nVal2;
NodeT *ptrNode;
bool bUpdate;

Triplets()
{
    cout << "creating empty triplet" << endl;
    nVal2 = 0;
    nVal1 = 0;
    bUpdate = false;
    ptrNode = NULL;
}

~Triplets()
{
    cout << "destroying triplet" << endl;
    delete ptrNode;
}

Triplets(int nVal1, int nVal2, bool bUpdate, NodeT *ptrNode)
{
    cout << "creating triple with values" << endl;
    this->nVal2 = nVal2;
    this->nVal1 = nVal1;
    this->bUpdate = bUpdate;
    this->ptrNode = ptrNode;
}
 };

この使用法

void find_triplets(NodeT *ptrRoot)
{


   if (ptrRoot != NULL)
    {
    find_triplets(ptrRoot->left);
    find_triplets(ptrRoot->right);


    cout << "find triplets and save them to the queue" << endl;
        cout << " we hit a hot spot is null the root, nothing to see here move along boys" << endl;

     if(ptrRoot->left != NULL && ptrRoot->right != NULL)
        {

        if (ptrRoot->left->done == true && ptrRoot->right->done == true)
        {
        cout << "we got one of 2 sons true so do something, this are the sons "<< ptrRoot->left->key_value << " " << ptrRoot->right->key_value << endl;         

        cout << "sum them and put it in the father and set it to true " << endl;
        ptrRoot->key_value = ptrRoot->left->key_value + ptrRoot->right->key_value;
        ptrRoot->done = true;


        cout << "thread queue " << endl;
        Triplets triplet(ptrRoot->left->key_value, ptrRoot->right->key_value, ptrRoot->done, ptrRoot);
        qThreads.push(triplet);
       }
     }
     }
    }

そしてこのメ​​イン

MyThread mt;
mt.start();
mt.run();
cout << "sum = " << mt.sum(&bt) << endl;
THREADS_HOW_MANY = boost::thread::hardware_concurrency();
std::cout << THREADS_HOW_MANY << std::endl;

while (!bt.Values_to_do.empty())
{
    cout << "da vals to sum are these " << bt.Values_to_do.front() << " and aready done " << bt.values_done.front() << endl;
    bt.Values_to_do.pop();
    bt.values_done.pop();
}

  while(!mt.qThreads.empty())
{
cout << "da triplet are " << mt.qThreads.front().nVal1 << " " <<mt.qThreads.front().nVal2 << " "
        << mt.qThreads.front().ptrNode << " " << mt.qThreads.front().bUpdate
        << endl;
mt.qThreads.pop();

}

このすべてが私にこの結果を与えます(明らかに間違っています)

  done lets chek
  find triplets
  find triplets and save them to the queue
   we hit a hot spot is null the root, nothing to see here move along boys
   find triplets and save them to the queue
    we hit a hot spot is null the root, nothing to see here move along boys
 find triplets and save them to the queue
 we hit a hot spot is null the root, nothing to see here move along boys
 we got one of 2 sons true so do something, this are the sons 2 8
 sum them and put it in the father and set it to true 
 thread queue 
 creating triple with values
 destroying triplet
 find triplets and save them to the queue
 we hit a hot spot is null the root, nothing to see here move along boys
 find triplets and save them to the queue
 we hit a hot spot is null the root, nothing to see here move along boys
find triplets and save them to the queue
we hit a hot spot is null the root, nothing to see here move along boys
 we got one of 2 sons true so do something, this are the sons 11 15
 sum them and put it in the father and set it to true 
thread queue 
creating triple with values
destroying triplet
find triplets and save them to the queue
 we hit a hot spot is null the root, nothing to see here move along boys
 we got one of 2 sons true so do something, this are the sons 19976608 19976464
 sum them and put it in 
 the father and set it to true 
 thread queue 
 creating triple with values
 destroying triplet
 the gran total is 19976320

今、私のqは、私のトリプレットオブジェクトがキューに追加されず、代わりに破棄されている理由です。これが、合計がすべて台無しになっている原因です。ありがとう。

4

2 に答える 2

0

Tl; dr-ポップアップするのは、メモリを管理するデストラクタがあることです。

~Triplets()
{
    cout << "destroying triplet" << endl;
    delete ptrNode;
}

まだコピーコンストラクタと代入演算子はありません。それらを実装してみてください。

于 2012-06-19T07:05:32.167 に答える
0

Tripletsクラスのコピーを作成しているようです。

Triplets triplet(ptrRoot->left->key_value, ptrRoot->right->key_value, ptrRoot->done, ptrRoot);
qThreads.push(triplet);

の宣言に応じて、最初にnamedqThreadsのインスタンスを作成し、次にそれをプッシュするとコピーされます。次に、スコープが(次の時点で)離れると、インスタンスは破棄されます。これはもちろん、その中のポインタとコピーが持っているポインタが無効になることを意味します。TripletstripletqThreads}tripletdelete

于 2012-06-19T07:15:15.843 に答える