简体   繁体   English

为什么我在两个线程中使用互斥锁保护 std::queue,仍然是 memory 泄漏

[英]why I use mutex protect std::queue in two thread, still memory leak

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为什么我在两个线程中使用互斥锁保护std :: queue,仍然memory泄漏Push比pop快,但是在2000次之后,push停止,pop一直在做,当队列为空时,这个程序的memory是1.3GiB , memory 泄漏在此处输入图像描述

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);
}

thx to Daniel Langr, I used感谢 Daniel Langr,我用过

malloc_trim(0);

solve this memory problem.解决这个 memory 问题。

class Test {
public:
    std::thread read_thread;
    std::thread write_thread;
    std::thread clear;


    mutable std::mutex mut;
    std::list<std::shared_ptr<std::vector<float>>> queue;
    void Init() {
        read_thread = std::thread(&Test::PushFunc, this);
        write_thread = std::thread(&Test::PopFunc, this);
        clear =  std::thread(&Test::ClearFunc, this);
    }

    void PushFunc()
    {
        int index = 0;
        while (true) {
            usleep(1000 * 1);
            auto p = std::make_shared<std::vector<float>>();
            for (int i = 0; i < 752; ++i) {
                for (int j = 0; j < 480; ++j) {
                    p->emplace_back(1.0f);
                }
            }
            std::unique_lock<std::mutex> a(mut);
            queue.emplace_back(p);
            std::cout << "push one: " << queue.size() << "\n";
            if (index++ > 2000) {
                break;
            }
        }
    }

    void PopFunc()
    {
        while (true) {
            usleep(1000 * 25);
            std::unique_lock<std::mutex> lk(mut);
            if (!queue.empty()) {
                queue.pop_front();
                std::cout << "pop one: " << queue.size() << "\n";
            } else {
                std::cout << "cannot pop\n";
            }
        }
    }

    void ClearFunc()
    {
        while (true) {
            sleep(1);
            malloc_trim(0);
        }
    }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM