在CThreadPool下从工作线程传输错误信息的最佳方法是什么?

时间:2022-01-19 15:59:00

I recently discovered ATL's CThreadPool class and was very happy with this find. It's a neat little class that will take care of the syncronization semantics of having multiple worker threads to process some queue of taks to do. The tasks are fed to the CThreadPool object, by some extrnal process.

我最近发现了ATL的CThreadPool类,对这个发现非常满意。这是一个简洁的小类,它将处理具有多个工作线程来处理某些队列的同步化语义。通过一些外部过程将任务提供给CThreadPool对象。

Now suppose one of the worker threads encounters an error, or has an exception. I don't mean the type of exceptions where you would want your whole application to just die, but it is something you want to do deal with more gracefully. By gracefully I mean the applicattion can die, but I need to do some clean up work and to put a propper message somewhere as to why it did.

现在假设其中一个工作线程遇到错误,或者有异常。我并不是指您希望整个应用程序只是死亡的异常类型,而是您想要更优雅地处理的事情。优雅地说,我的意思是应用程序可能会死,但我需要做一些清理工作并在某处发布一个propper消息,告诉它为什么会这样做。

What is the best way to transfer the error info (message, error code, source etc.) to the main thread owning the CThreadPool object?

将错误信息(消息,错误代码,源等)传递给拥有CThreadPool对象的主线程的最佳方法是什么?

2 个解决方案

#1


If I was working with this system, I would PostMessage from the erroneous task with a custom event that your application's message handler can respond to. You should bundle up whatever information you need in order to properly clean up.

如果我正在使用这个系统,我会使用应用程序的消息处理程序可以响应的自定义事件从错误的任务PostMessage。您应该捆绑所需的任何信息,以便正确清理。

#2


Use queue with synchronization access for logging all errors and thread for consumer events at queue.

使用具有同步访问权限的队列来记录队列中的使用者事件的所有错误和线程。

#1


If I was working with this system, I would PostMessage from the erroneous task with a custom event that your application's message handler can respond to. You should bundle up whatever information you need in order to properly clean up.

如果我正在使用这个系统,我会使用应用程序的消息处理程序可以响应的自定义事件从错误的任务PostMessage。您应该捆绑所需的任何信息,以便正确清理。

#2


Use queue with synchronization access for logging all errors and thread for consumer events at queue.

使用具有同步访问权限的队列来记录队列中的使用者事件的所有错误和线程。