I am working on a program which is essentially single-threaded, and its only thread is the main event-loop thread. Consequently, all its data structures are basically not protected by anything like critical region.
我正在开发一个基本上是单线程的程序,它唯一的线程是主事件循环线程。因此,它的所有数据结构基本上都不受关键区域的保护。
Things work fine until it recently integrates some new functions based on DirectShow API. Some DirectShow APIs open a second event-loop and within that second loop it dispatch messages (i.e. invoke other event-handling callbacks unpredictably). So when a second event-handling function is invoked, it might damage the data struct which is being accessed by the function that invokes the DirectShow API.
在最近集成了一些基于DirectShow API的新功能之前,事情一切正常。一些DirectShow API打开第二个事件循环,并在第二个循环内调度消息(即,不可预测地调用其他事件处理回调)。因此,当调用第二个事件处理函数时,它可能会损坏调用DirectShow API的函数正在访问的数据结构。
I have some experience in kernel programming. And what comes in my mind is that, for a single-threaded program, how it should deal with its data structure is very like how kernel should deal with per-CPU data structure. And in kernel, when a function accesses per-CPU data, it must disable the interrupt (very like the message-dispatching in a second event-loop). However, I find there is no easy way to either avoid invoke DirectShow API or to prevent the create of a second event-loop within them, is there any way?
我在内核编程方面有一些经验。我想到的是,对于单线程程序,它应该如何处理它的数据结构就像内核应该如何处理每个CPU的数据结构一样。在内核中,当一个函数访问每个CPU数据时,它必须禁用中断(非常类似于第二个事件循环中的消息调度)。但是,我发现没有简单的方法可以避免调用DirectShow API或阻止在其中创建第二个事件循环,有什么办法吗?
2 个解决方案
#1
There are several possible solutions that come to mind, depending on exactly what's going wrong and your code:
根据确切的错误和代码,可以想到几种可能的解决方案:
- Make sure your data structures are in a consistent state before calling any APIs that run a modal loop.
- If that's not possible, you can use a simple boolean variable to protect the structure. If it's set, then simply abort any attempt to update it or queue the update for later. Another option is to abort the previous operation.
- If the problem is user generated events, then disable the problematic menus or buttons while the operation is in progress. Alternatively, you could display a modal dialog.
在调用运行模态循环的任何API之前,请确保您的数据结构处于一致状态。
如果那是不可能的,您可以使用一个简单的布尔变量来保护结构。如果已设置,则只需中止任何更新它的尝试或将更新排队等待以后。另一种选择是中止先前的操作。
如果问题是用户生成的事件,则在操作过程中禁用有问题的菜单或按钮。或者,您可以显示模态对话框。
#2
mutexes. semaphores. locking. whatever name you want to call it, that's what you need.
互斥。信号灯。锁定。无论你想叫什么名字,这都是你需要的。
#1
There are several possible solutions that come to mind, depending on exactly what's going wrong and your code:
根据确切的错误和代码,可以想到几种可能的解决方案:
- Make sure your data structures are in a consistent state before calling any APIs that run a modal loop.
- If that's not possible, you can use a simple boolean variable to protect the structure. If it's set, then simply abort any attempt to update it or queue the update for later. Another option is to abort the previous operation.
- If the problem is user generated events, then disable the problematic menus or buttons while the operation is in progress. Alternatively, you could display a modal dialog.
在调用运行模态循环的任何API之前,请确保您的数据结构处于一致状态。
如果那是不可能的,您可以使用一个简单的布尔变量来保护结构。如果已设置,则只需中止任何更新它的尝试或将更新排队等待以后。另一种选择是中止先前的操作。
如果问题是用户生成的事件,则在操作过程中禁用有问题的菜单或按钮。或者,您可以显示模态对话框。
#2
mutexes. semaphores. locking. whatever name you want to call it, that's what you need.
互斥。信号灯。锁定。无论你想叫什么名字,这都是你需要的。