代码:
- mtx.lock();
- printf("node %d push localW prepared,and local flag=%d,clock=%d\n",procID,localW.getFlag(),localW.getClock());
- printf("localQ is empty:%s\n",localQ.empty()?"true":"false");
- localQ.push(localW);
- printf("node %d push localW successfully,and front flag=%d,clock:%d\n",procID,localQ.front().getFlag(),localQ.front().getClock());
- printf("node %d push localW successfully,and local flag=%d,clock:%d\n",procID,localW.getFlag(),localW.getClock());
- mtx.unlock();
输出的结果就神奇了,给大家瞅瞅:
node 2 push localW prepared,and local flag=0,clock=0
localQ is empty:true
node 2 push localW successfully,and front flag=4,clock:24
node 2 push localW successfully,and local flag=0,clock:0
目前还未找到原因,找到原因我会更新。
---------------------------------------------------update-----------------------------------------------------------
虽然没有确切地找出是什么原因,但是根据一个程序员的经验,估计应该是因为压入的对象过于庞大,所以直接压对象,会导致压入失败。
因此,这一问题可以换一个思路来解决,这个思路也很有效,那就是压地址。
把作为对象的队列,变成作为对象地址的队列,这样队列的进出就只有地址,地址我们知道是一个很小的数据,因此肯定不会出错,至此问题解决。
新代码:
- mtx.lock();
- printf("node %d push localW prepared,and local flag=%d,clock=%d\n",procID,localW.getFlag(),localW.getClock());
- printf("localQ is empty:%s,queue size:%d\n",localQ.empty()?"true":"false",localQ.size());
- localQ.push(&localW);
- printf("node %d push localW successfully,and front flag=%d,clock:%d\n",procID,localQ.back()->getFlag(),localQ.back()->getClock());
- printf("node %d push localW successfully,and local flag=%d,clock:%d\n",procID,localW.getFlag(),localW.getClock());
- mtx.unlock();
新队列声明代码:
- queue<TheMatrix *> localQ;
- queue<TheMatrix *> globalQ;
新结果:
node 1 push localW prepared,and local flag=0,clock=0
localQ is empty:true,queue size:0
node 1 push localW successfully,and front flag=0,clock:0
node 1 push localW successfully,and local flag=0,clock:0