#include<iostream>
#include<vector>
#include<thread>
#include<string>
using namespace std;
vector<string> s;
void add()
{
while(true)
{
getchar();
s.push_back("added");
}
}
void show()
{
while(true)
{
//cout<<"";
while(!s.empty())
{
cout<<(*s.begin())<<endl;
s.erase(s.begin());
}
}
}
int main()
{
thread one(add);
thread two(show);
one.join();
two.join();
}
在调试模式下,不存在这样的问题。在发布模式下,如果注释行未注释,则它会再次运行。但就像这样,就有一个问题。问题是什么?在发布模式下,vector :: empty()函数无法正常工作
您的代码被破坏,因为您有两个线程操纵矢量而没有同步。使用互斥锁。 –
但它适用于{cout <<“”;} – sunofkyuss
它看起来可行,但那不可靠。严重的是,获得关于多线程的教程,它会解释一些事情。 –