boost::lockfree::队列似乎没有释放内存,尽管每个集合对象上的析构函数都被调用
boost::lockfree::queue doesn't seem to free memory though destructor on every collection object is invoked
这里的问题是,boost::lockfree::queue在分配内存后不会释放内存。为什么自由列表节点没有返回到操作系统?调用单个集合对象上的析构函数。
我想解决内存问题,关于无锁定队列内存分配的任何建议。我在这里做错了什么?
#include <iostream>
#include <Windows.h>
#include <boost/thread/thread.hpp>
#include <boost/lockfree/queue.hpp>
using namespace std;
using namespace boost;
struct Record
{
char str[128];
Record(const char* rec)
{
memset(this->str, 0, sizeof(this->str));
strcpy_s(this->str, rec);
}
~Record()
{
cout << "~Record " << this->str << endl;
}
Record& operator= (const Record& rec)
{
if (this == &rec)
{
return *this;
}
memset(this->str, 0, sizeof(this->str));
strcpy_s(this->str, rec.str);
return *this;
}
};
typedef boost::lockfree::queue<Record*, boost::lockfree::fixed_sized<true>> RecordsQueue;
RecordsQueue Records(10000);
class MyClass
{
public:
void FillThread()
{
int i = 0;
while (true)
{
Record *rec = new Record(to_string(i).c_str());
Records.push(rec);
i++;
};
}
void ProcessThread()
{
while (true)
{
Record *rec;
Records.pop(rec);
{
cout << "Record " << rec->str << endl;
delete rec;
}
};
}
};
int _tmain(int argc, _TCHAR* argv[])
{
boost::thread* thread1, *thread2;
MyClass myObj;
thread1 = new boost::thread(boost::bind(&MyClass::FillThread, myObj));
HANDLE threadHandle1 = thread1->native_handle();
SetThreadPriority(threadHandle1, THREAD_PRIORITY_NORMAL);
boost::this_thread::sleep(boost::posix_time::seconds(1));
thread2 = new boost::thread(boost::bind(&MyClass::ProcessThread, myObj));
HANDLE threadHandle2 = thread2->native_handle();
SetThreadPriority(threadHandle2, THREAD_PRIORITY_NORMAL);
thread1->join();
thread2->join();
return 0;
}
看起来您向队列中推送的记录比可用空间多。这会覆盖以前拥有的空间,并导致这些空间将来不会被释放。尝试将FillThread更改为for循环。
相关文章:
- 保留计时器集合(对象与指针)的最佳方法
- 在具有自定义对象的集合中查找值
- 如何处理C 中对象的混合集合
- 如何创建异质对象的集合
- 如果对象尚未在集合中,则创建一个对象
- C++内存中的大型对象集合是否正常
- C++集合对象和 iStream 文件输入
- 只有Arduino的第一个对象在包含在另一个对象的集合/数组中时会丢失其数据属性值
- C 11按几个属性的自定义对象集合
- 位掩码:通过集合方法设置对象的不同状态
- 可以在C++中更改存储在集合中的对象的非关键部分.如何在java中进行类似操作
- 具有不同接口的对象的集合
- 集合中的不可变对象(C++ 和 Qt)
- C++维护子类对象的混合集合
- 无法将对象上的指针添加到集合
- 映射/集合迭代器不兼容-检查对象是否在映射中
- 在派生类中处理派生对象集合的最佳方法是什么?
- 设计模式-在C++中返回对象集合的函数
- 访问父类对象集合的成员的子类函数
- 在c++中,两个线程/对象在一个对象集合中可以安全地做什么?