CPP STL queue multithreaded error message: dereferencable

as follows, the main thread calls push () , and the second thread front () reads the head of the queue, and the team is busy when it is empty. As a result, line 11 front () reported an error deque iterator not dereferencable . After taking a look at the implementation of STL deque, I feel that the two operations should not conflict.

-sharpinclude "stdafx.h"

using namespace std;

queue<int>Q;
bool going = true;

void thread1() {
    while (going || !Q.empty()) {
        while (!Q.empty()) {
            int x = Q.front(); Q.pop();
        }
    }
}

int main()
{
    thread th(thread1);
    for (int i = 1; i <= 10000; iPP)
        Q.push(i);
    going = false;
    th.join();
    return 0;
}
Dec.31,2021
The containers in

stl are not thread safe, so be careful to lock them when using them

Menu