私はこのコードでC ++に奇妙な問題があります:
mutex_type list_mutex;
typedef list<char*> RQueue;
RQueue rQueue;
RQueue::reverse_iterator rstart, rend, last;
1 while(true) {
2 LockMutex(list_mutex);
3 rstart = rQueue.rbegin();
4 rend = rQueue.rend();
5 while( (rstart != rend) && (rstart != last) ) {
6 print *rstart;
7 }
8 last = rQueue.rbegin();
9 UnlockMutex(list_mutex);
10 }
rQueue
逆の順序で反復するキューですrQueue
いつでもメッセージを受け取ることができますlast
6 行目で受信メッセージの再処理を避けるためにイテレータを追加しました8 行目では、メッセージを出力した位置を保持し、最後のメッセージよりも新しいメッセージのみを出力したいと考えています。
私の問題: 反復が終了し、新しいメッセージがキューに追加されると、 iterator の値
last
が変更され、 iterator の値と同じになるrstart
ため、新しく到着したメッセージは 6 行目に出力されません。
last = rQueue.rbegin()
キューのロックを解除した後に新しい要素を受け取ると、値が変更される理由がわかりません。
ありがとう。