why I use mutex protect std::queue in two thread, still memory leak Push is faster than pop, but push after 2000 times, push is stop, pop is always doing, when queue is empty, the memory of this program is 1.3GiB, memory leak enter image description here
class Test {
public:
std::thread read_thread;
std::thread write_thread;
mutable std::mutex mut;
std::queue<std::shared_ptr<std::vector<float>>> queue;
void Init() {
read_thread = std::thread(&Test::PushFunc, this);
write_thread = std::thread(&Test::PopFunc, this);
}
void PushFunc()
{
int index = 0;
while (true) {
usleep(1000 * 1);
std::lock_guard<std::mutex> a(mut);
std::vector<float> sig;
for (int i = 0; i < 752; ++i) {
for (int j = 0; j < 480; ++j) {
sig.emplace_back(1.0f);
}
}
queue.push(std::make_shared<std::vector<float>>(sig));
std::cout << "push one\n";
if (index++ > 2000) {
break;
}
}
}
void PopFunc()
{
while (true) {
usleep(1000 * 25);
std::lock_guard<std::mutex> lk(mut);
if (!queue.empty()) {
queue.pop();
std::cout << "pop one\n";
} else {
std::cout << "cannot pop\n";
}
}
}
};
int main ()
{
Test t;
t.Init();
while (true);
}