What's the best/proper method to collect log messages from multiple threads and have them all be displayed using a window? (while the threads are running).
从多个线程收集日志消息并使用窗口显示它们的最佳/正确方法是什么? (当线程正在运行时)。
I am currently trying to redirect stdout (cout) in to a wxTextCtrl but failing miserably when attempting to do so over multiple threads. Any help would be appreciated.
我目前正在尝试将stdout(cout)重定向到wxTextCtrl,但在尝试通过多个线程执行此操作时失败了。任何帮助,将不胜感激。
2 个解决方案
#1
Logging has had a few major updates recently in the wxWidgets trunk, you can read about them here. One of them is to add support for logging from threads other than the main thread.
最近在wxWidgets主干中记录了几个主要的更新,你可以在这里阅读它们。其中之一是添加对主线程以外的线程进行日志记录的支持。
#2
In what way is it failing? I'm not familiar with the wxTextCtrl, but unless it has built in synchronization (ie. its thread safe) that could be a big issue. The simplest way to protect a single resource like this is via a named 'mutex'. The following example is what you can do in each thread to make sure that only one accesses this resource (the output window) at a time.
它以什么方式失败?我不熟悉wxTextCtrl,但除非它内置同步(即它的线程安全),这可能是一个大问题。保护这样的单个资源的最简单方法是通过命名的“互斥”。以下示例是您可以在每个线程中执行的操作,以确保一次只能访问此资源(输出窗口)。
// In each thread's initialization:
HANDLE mutexHandle = CreateMutex(0,FALSE,"__my_protecting_mutex__");
// Whenever you use the debug output:
WaitForSingleObject(mutexHandle, /* Timeout if you like. */ 0xFFFFFFFF );
// Do our printing here.
ReleaseMutex(mutexHandle);
// In each thread's cleanup:
CloseHandle(mutexHandle);
So this basically guarantees that only one thread can be in between the wait and the release. Now if your issue is actually routing to the wxTextCtrl, I would need some more details.
所以这基本上保证了在等待和释放之间只能有一个线程。现在,如果您的问题实际上是路由到wxTextCtrl,我需要更多细节。
Edit: I just realized that what I posted is Windows specific, and maybe you aren't on windows! If you aren't I don't have experience with other platform's synchronization methods, but boost has some generic libraries which are not platform specific.
编辑:我刚刚意识到我发布的内容是Windows特定的,也许你不在Windows上!如果您不是,我没有其他平台的同步方法的经验,但是boost有一些非平台特定的通用库。
#1
Logging has had a few major updates recently in the wxWidgets trunk, you can read about them here. One of them is to add support for logging from threads other than the main thread.
最近在wxWidgets主干中记录了几个主要的更新,你可以在这里阅读它们。其中之一是添加对主线程以外的线程进行日志记录的支持。
#2
In what way is it failing? I'm not familiar with the wxTextCtrl, but unless it has built in synchronization (ie. its thread safe) that could be a big issue. The simplest way to protect a single resource like this is via a named 'mutex'. The following example is what you can do in each thread to make sure that only one accesses this resource (the output window) at a time.
它以什么方式失败?我不熟悉wxTextCtrl,但除非它内置同步(即它的线程安全),这可能是一个大问题。保护这样的单个资源的最简单方法是通过命名的“互斥”。以下示例是您可以在每个线程中执行的操作,以确保一次只能访问此资源(输出窗口)。
// In each thread's initialization:
HANDLE mutexHandle = CreateMutex(0,FALSE,"__my_protecting_mutex__");
// Whenever you use the debug output:
WaitForSingleObject(mutexHandle, /* Timeout if you like. */ 0xFFFFFFFF );
// Do our printing here.
ReleaseMutex(mutexHandle);
// In each thread's cleanup:
CloseHandle(mutexHandle);
So this basically guarantees that only one thread can be in between the wait and the release. Now if your issue is actually routing to the wxTextCtrl, I would need some more details.
所以这基本上保证了在等待和释放之间只能有一个线程。现在,如果您的问题实际上是路由到wxTextCtrl,我需要更多细节。
Edit: I just realized that what I posted is Windows specific, and maybe you aren't on windows! If you aren't I don't have experience with other platform's synchronization methods, but boost has some generic libraries which are not platform specific.
编辑:我刚刚意识到我发布的内容是Windows特定的,也许你不在Windows上!如果您不是,我没有其他平台的同步方法的经验,但是boost有一些非平台特定的通用库。