64,670
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
#include <list>
#include <thread>
using namespace std;
list<int> l;
bool stop = false;
void TheadFunction()
{
while (true)
{
do
{
if (!l.empty())
{
auto element = l.front();
l.pop_front();
}
else
{
break;
}
} while (true);
if (stop)
{
break;
}
this_thread::sleep_for(chrono::milliseconds(1));
}
}
int main()
{
thread* t = new std::thread(&TheadFunction);
int count = 0;
while (count < 1000000)
{
for (int i = 0;i< 1000;++i)
{
l.push_back(i);
}
this_thread::sleep_for(chrono::milliseconds(1));
count++;
}
stop = true;
if (t->joinable())
{
t->join();
}
delete t;
t = nullptr;
}
你好,请问您这个问题解决了吗?我再学习的时候,也是开两个线程对同一个list读写操作,看的教学视频在win上回报错,但是我再linux上不会,现在有点疑惑。