提高::线程造成小事件句柄泄漏? [英] boost::thread causing small event handle leak?

查看:157
本文介绍了提高::线程造成小事件句柄泄漏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我调试该数据库项目。它包装访问S​​QLite,让更高级别的应用程序。它的设计异步运行,也就是说,它有一个像ExecuteRequestAsync()和IsRequestReady方法(i)。当ExecuteRequestAsync被调用时,它产生一个boost ::线程来完成这项工作,并立即返回函数。当更高级别的应用程序决定它不再想运行的请求的结果,它可以调用姆prequest()来取消它。因为它是难以优雅取消数据库请求,姆prequest的实现只是保持清理监视线程是等待已完成的请求,并删除它们。所有的boost ::线程通过升压:: shared_ptr的管理,如:

I'm debugging this database project. It wraps access to SQLite for a higher level application. It's designed to run asynchronously, that is, it has methods like ExecuteRequestAsync() and IsRequestReady(). When ExecuteRequestAsync is called, it spawns a boost::thread to do the job and return the function immediately. When the higher level application decides that it no longer wants the result of a running request, it may call DumpRequest() to cancel it. Since it's difficult to gracefully cancel a database request, the implementation of DumpRequest just maintain a "cleanup monitor thread" that waits for "finished requests" and remove them. All boost::threads are managed through boost::shared_ptr, like:

boost::shared_ptr<boost::thread> my_thread = new boost::thread(boost::bind(&DBCon::RunRequest, &this_dbcon));

和当它不再需要(被取消):

And when it's no longer needed (to be canceled):

vector<boost::shared_ptr<boost::thread> > threads_tobe_removed;
// some iteration
threads_tobe_removed[i].get()->join();
threads_tobe_removed.erase(threads_tobe_removed.begin()+i);

我创造了这个单元测试项目,测试执行和倾倒请求的机制。它运行的请求,并随机取消运行的请求,并重复几千遍。该机制被证明是好的。一切都如预期。

I created this unit test project to test the mechanism of executing and dumping the requests. It runs requests and randomly cancels running requests, and repeats for several thousand passes. The mechanism turned out to be okay. Everything worked as expected.

然而,通过观察通过sysinternal的进程资源管理器的单元测试项目,它的发现,有一个句柄泄漏问题。每500上下的推移,句柄计数增加1,永不返回了。它的事件类型句柄正在增加。文件和线程处理不增加(句柄当然#正在增加的线程都产生了,但有一个睡眠(10000)调用每百通行证等他们进行清理,使手柄计数可观察到的)。

However, through observing the unit test project through sysinternal's Process Explorer, it's discovered that there's a handle leak problem. Every 500-ish passes, the handle count increases by 1, and never returns back. It's the "Event" type handle that is increasing. File and thread handles are not increasing (of course # of handles are increasing as threads are spawned, but there is a Sleep(10000) call every hundred passes to wait for them to be cleaned up so that the handle count can be observed).

我没有被管理事件处理自己。它们是由升压::线程一旦创建的线程的创建。我只保证正常关闭线程,我不知道是用来做什么的事件。

I haven't been managing Event handles myself. They are created by boost::thread upon the creation of the thread. I only guarantee to gracefully close the threads, I have no idea what the Events are used for.

我不知道是否有人已经经历过类似的问题?什么可能是这个泄漏的原因是什么?这是数量的Process Explorer可靠,足以称之为一个句柄泄漏?有什么办法来跟踪和解决它?

I'm wondering if anyone has experienced similar problems? What might be the cause of this leak? Is this number in Process Explorer reliable enough to call it a handle leak? Is there any way to trace and fix it?

我使用静态链接的1.40提升在Windows Vista上,用Visual C ++。

I'm using statically linked boost 1.40 on Windows Vista, with Visual C++.

推荐答案

是访问 threads_tobe_removed 线程安全的?如果没有,有可能是一个竞争条件,当一个线程增加了一个线程通过调用该载体姆prequest ,而清理监视线程删除线程从载体。因此,的boost ::线程 -objects不先加入线程可能会被破坏,这将使该线程没有关联的对象中运行,这或许可以解释的泄漏。

Is the access to threads_tobe_removed thread-safe? If not, there may be a race condition, when one thread adds a thread to the vector via a call to DumpRequest, while the cleanup monitor thread deletes a thread from the vector. Thus, boost::thread-objects may be destroyed without joining the thread first, which would leave the thread running without an associated object, which might explain the leak.

这篇关于提高::线程造成小事件句柄泄漏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆