我试图使程序的目标是将元素添加到队列(在线程中)并显示有关队列的数据(您可以在主窗口中看到要显示的数据)。在此之前,我想从队列中删除一个元素(每两秒)并添加新元素(每一秒)。
#include <iostream>
#include <queue>
#include <thread>
#include <Windows.h>
using std::queue;
using std::cout;
void loadQueue(queue<int> &toLoad)
{
for(int i = 0; i < 100; i++)
{
toLoad.push(i);
Sleep(1000);
}
}
int main(void)
{
queue<int>toLoad;
std::thread(loadQueue, std::ref(toLoad));
while(true)
{
cout << "SIZE OF QUEUE : " << toLoad.size() << '\n' << '\n';
cout <<"FRONT :" << toLoad.front() << '\n' << '\n';
cout <<"BACK : " << toLoad.back() << '\n';
toLoad.pop();
Sleep(2000);
}
}
Run Code Online (Sandbox Code Playgroud)
当我启动程序时,我什么也看不到。程序立即关闭。Visual Studio 向我显示此消息: