小编Mic*_*ael的帖子

C++ 中的队列和线程

我试图做什么:

我试图使程序的目标是将元素添加到队列(在线程中)并显示有关队列的数据(您可以在主窗口中看到要显示的数据)。在此之前,我想从队列中删除一个元素(每两秒)并添加新元素(每一秒)。

代码

#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 向我显示此消息:

启动程序后 VISUAL STUDIO 代码显示错误

c++ queue multithreading

0
推荐指数
1
解决办法
122
查看次数

标签 统计

c++ ×1

multithreading ×1

queue ×1