切换线程以进行 MFC 应用程序清理
我正在尝试清理由特定线程创建的特定内存对象(因此只能由该线程访问)。我实现这一目标的唯一方法是在释放该内存块时切换到该特定线程。
这就是我分配特定内存上下文的方式: http://imagebin.ca/img/S6mwZBFu.jpg
这就是我试图做的:
替代文本 http://imagebin.ca/img/DeTe9Z6h.jpg
我最初添加内存上下文的创建和销毁方式如下:
int Thread2::main()
{
CudaMemoryContext *theCudaObj = new CudaMemoryContext();
while(!TerminateStatus())
{
...
}
delete theCudaObj;
return 0;
}
但是,这种方法效果不佳,即当我清理“delete theCudaObj;”时,程序崩溃了。线。我想知道是否可以在清理时切换活动线程,或者分配 CUDA 上下文以供两个线程访问,以便我可以通过两个线程轻松清理和访问它。预先感谢您的建议。
I'm trying to clean up specific memory objects created by a specific thread (hence only accessible to that thread). The only way for me to achieve that is to switch to that particular thread when freeing that memory block.
This is how I allocated the specific memory context:
http://imagebin.ca/img/S6mwZBFu.jpg
This is what I attempted to do:
alt text http://imagebin.ca/img/DeTe9Z6h.jpg
I have originally added the memory context creation and destruction in a manner like the following:
int Thread2::main()
{
CudaMemoryContext *theCudaObj = new CudaMemoryContext();
while(!TerminateStatus())
{
...
}
delete theCudaObj;
return 0;
}
However, this approach is not working very well, i.e. the program crashes right when I'm cleaning up on the "delete theCudaObj;" line. I'm wondering if I can switch active threads when cleaning up, or allocate the CUDA context to be accessible by both threads so that I can clean up and access it with ease through both threads. Thanks in advance for suggestions.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Thread#1 是如何销毁 Thread#2 的?通常最好通知线程自行终止,而不是使用 TerminateThread()。
How is Thread#1 destroying Thread#2? It's typically best to signal a thread to terminate itself and not use TerminateThread().
您最初的方法看起来像是处理问题的正确方法 - 当线程收到终止信号时,它会停止循环并清理所有分配的内存。
在您向线程发出终止信号的上下文中,请确保在允许应用程序退出之前等待它退出。过早退出可能会导致您的崩溃。使用连接的调试器运行并设置为在抛出异常时中断以进行诊断。
Your original approach looks like the right way to go about things - when the thread is signaled to terminate it stops looping and cleans up any allocated memory.
In the context where you signal a thread to terminate, be sure you wait for it to exit before allowing the application to exit. Premature exit could have been causing your crash. Run with the debugger attached and set to break when exceptions are thrown to diagnose.