【发布时间】:2014-12-30 06:52:07
【问题描述】:
我实现了这个讨论Boost group_threads Maximal number of parallel thread中描述的线程池
通过一个更改,我有一个函数等待所有线程完成:
class thread_pool
{
private:
mutex mx;
condition_variable cv;
typedef function<void()> job_t;
std::deque<job_t> _queue;
thread_group pool;
boost::atomic_bool shutdown;
static void worker_thread(thread_pool& q)
{
while (optional<job_t> job = q.dequeue())
(*job)();
}
public:
thread_pool() : shutdown(false) {
//LOG_INFO_MESSAGE << "Number of possible Threads: " << boost::thread::hardware_concurrency() << std::endl;
for (unsigned i = 0; i < boost::thread::hardware_concurrency(); ++i){
pool.create_thread(bind(worker_thread, ref(*this)));
}
}
void enqueue(job_t job)
{
lock_guard<mutex> lk(mx);
_queue.push_back(job);
cv.notify_one();
}
optional<job_t> dequeue()
{
unique_lock<mutex> lk(mx);
namespace phx = boost::phoenix;
cv.wait(lk, phx::ref(shutdown) || !phx::empty(phx::ref(_queue)));
if (_queue.empty())
return none;
job_t job = _queue.front();
_queue.pop_front();
return job;
}
void WaitTillAllJobsAreDone(){
shutdown = true;
{
lock_guard<mutex> lk(mx);
cv.notify_all();
}
pool.join_all();
}
~thread_pool()
{
shutdown = true;
{
lock_guard<mutex> lk(mx);
cv.notify_all();
}
pool.join_all();
}
};
我的用法:
class Foo{
public:
Foo(std::vector<boost::shared_ptr<Class B> > data):m_data(data),m_maxDepth(5)
{
}
void initializateThreads(){
thread_pool threadPool;
std::vector<std::vector<double> > result(m_data.size());
std::vector<std::vector<double> >::iterator it;=result.begin();
for(auto d:m_data){
threadPool.enqueue(boost::bind(&Foo::Work,this,d,boost::ref(*it))):
++it;
}
threadPool.WaitTillAllJobsAreDone();
//do something with results;
}
void Work(boost::shared_ptr<Class B> ptr,std::vector<double>& resultThread,int currentDepth){
if(currentDepth>m_maxDepth) return;
//do some calculation with ptr and add it to resultThread
resultThread.push_back((double) some result of B);
Work(ptr,resultThread,(currentDepth+1));
}
}
当我使用我的线程池时,我的程序在本节之后使用了大量内存并且从不释放它。没有线程池,我对相同的功能没有任何问题。线程池有错误吗?我必须释放创建的线程?
--------- 编辑 --------------
int main()
{
{
std::vector<Class B> data; //filled
Foo foo(data);
foo.LoadSomedata();
foo.initializateThreads();
} //< all memory should be freed or ?
while(1){}//< let process alive but memory in ressource manager should be very small or?
}
我写了这个测试程序。 Valgrind 还说没有内存泄漏。当我的程序在 while 循环中时,我的进程的内存应该非常小还是?但在系统监视器上,它有 3GB 用于此过程。我是不是脑子有误?
VALGrind 输出:
==24210== HEAP SUMMARY:
==24210== in use at exit: 0 bytes in 0 blocks
==24210== total heap usage: 2,055,546 allocs, 2,055,546 frees, 220,359,375 bytes allocated
==24210==
==24210== All heap blocks were freed -- no leaks are possible
==24210==
==24210== For counts of detected and suppressed errors, rerun with: -v
==24210== Use --track-origins=yes to see where uninitialised values come from
==24210== ERROR SUMMARY: 8964228 errors from 69 contexts (suppressed: 2 from 2)
我有一些已知的未初始化指针是错误的。
【问题讨论】:
-
让它成为一个独立的例子。您的“我的用法”代码零意义,句号。这是什么是:
this,vector,boost sharedPtr,boost::ref(vector[i]::iterator), 0)? -
sry i 虽然函数的参数并不重要。我编辑了这个,希望现在能更好地理解。我有每个 Worker 的结果向量,它会被修改
-
当然,如果参数持有 resources 并且您抱怨 resources 泄漏,这并非不重要。
-
这是真的,但如果我不使用 poolThread,这适用于相同的功能和参数
-
我们无法重现该问题。您不断发布无效代码。在 C++ 中没有
Class B这样的东西。我不知道你希望我们做什么。你必须弄清楚,直到你能提出一个有效的问题。
标签: c++ multithreading boost memory-leaks