队列没有保存更新的数据
Queue not holding the updated data
我有一个简单的多线程程序,其中一个线程正在填充队列,而另一个线程正在使用队列,我知道这个示例程序有问题,它正试图复制我的应用程序。我没有解释为什么会这样,我希望输出会弹出 1 、 2 等
#include <iostream>
#include <memory>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <queue>
using namespace std;
class A{
public :
int y;
A(int z):y(z){
cout << "constructed =" << y << std::endl;
}
~A(){
cout << "destructed =" << y << std::endl;
}
};
std::mutex lk;
std::queue<shared_ptr<A>> p;
std::condition_variable cv;
int ct = 1;
void waitfordata(){
std::unique_lock<std::mutex> lock(lk);
cv.wait(lock, []{return p.size();});
cout << "popping = " << p.front()->y << endl;
p.pop();
}
void threadhandler(){
while(1){
waitfordata();
}
}
int main()
{
std::thread foo(threadhandler);
std::shared_ptr<A> svc_resp = std::make_shared<A>(ct++);
while(1){
{
std::lock_guard<std::mutex> lock(lk);
p.push(svc_resp);
}
cv.notify_one();
std::this_thread::sleep_for(std::chrono::seconds(1));
std::shared_ptr<A> svc_resp = std::make_shared<A>(ct++);
}
return 0;
}
Ouput which I got
constructed =1
popping = 1
constructed =2
destructed =2
popping = 1
constructed =3
destructed =3
popping = 1
constructed =4
destructed =4
popping = 1
Ouput which I was expecting
constructed =1
destructed =1 <===== why is 1 not destructed
popping = 1
constructed =2
destructed =2
popping = 2 <===== why is this not taking the next data
constructed =3
destructed =3
popping = 3 <===== why is this not taking the next data
constructed =4
destructed =4
popping = 4 <===== why is this not taking the next data
您在 main
中有 两个 个名为 svc_resp
的变量。第二个(在 while
循环的底部)将创建并短暂拥有一个 shared_ptr<A>
,然后立即销毁它。这说明了随着数量的增加而构造和破坏的输出。
第一个 svc_resp
变量,在 while
循环之前声明,作为 1
的 y
并且在每个循环中 re-pushed 到您的队列。
修复很简单:从第二个 svc_resp
声明中删除类型名称,将其变成赋值。
svc_resp = std::make_shared<A>(ct++);
我有一个简单的多线程程序,其中一个线程正在填充队列,而另一个线程正在使用队列,我知道这个示例程序有问题,它正试图复制我的应用程序。我没有解释为什么会这样,我希望输出会弹出 1 、 2 等
#include <iostream>
#include <memory>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <queue>
using namespace std;
class A{
public :
int y;
A(int z):y(z){
cout << "constructed =" << y << std::endl;
}
~A(){
cout << "destructed =" << y << std::endl;
}
};
std::mutex lk;
std::queue<shared_ptr<A>> p;
std::condition_variable cv;
int ct = 1;
void waitfordata(){
std::unique_lock<std::mutex> lock(lk);
cv.wait(lock, []{return p.size();});
cout << "popping = " << p.front()->y << endl;
p.pop();
}
void threadhandler(){
while(1){
waitfordata();
}
}
int main()
{
std::thread foo(threadhandler);
std::shared_ptr<A> svc_resp = std::make_shared<A>(ct++);
while(1){
{
std::lock_guard<std::mutex> lock(lk);
p.push(svc_resp);
}
cv.notify_one();
std::this_thread::sleep_for(std::chrono::seconds(1));
std::shared_ptr<A> svc_resp = std::make_shared<A>(ct++);
}
return 0;
}
Ouput which I got
constructed =1
popping = 1
constructed =2
destructed =2
popping = 1
constructed =3
destructed =3
popping = 1
constructed =4
destructed =4
popping = 1
Ouput which I was expecting
constructed =1
destructed =1 <===== why is 1 not destructed
popping = 1
constructed =2
destructed =2
popping = 2 <===== why is this not taking the next data
constructed =3
destructed =3
popping = 3 <===== why is this not taking the next data
constructed =4
destructed =4
popping = 4 <===== why is this not taking the next data
您在 main
中有 两个 个名为 svc_resp
的变量。第二个(在 while
循环的底部)将创建并短暂拥有一个 shared_ptr<A>
,然后立即销毁它。这说明了随着数量的增加而构造和破坏的输出。
第一个 svc_resp
变量,在 while
循环之前声明,作为 1
的 y
并且在每个循环中 re-pushed 到您的队列。
修复很简单:从第二个 svc_resp
声明中删除类型名称,将其变成赋值。
svc_resp = std::make_shared<A>(ct++);