C ++ / CLI线程同步,包括托管和非托管代码 [英] C++/CLI Thread synchronization including managed and unmanaged code

查看:215
本文介绍了C ++ / CLI线程同步,包括托管和非托管代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理非托管算法类的包装类。我来到一个点,我需要单独的线程来处理和即时显示的结果。

I am working on a wrapper class for an unmanaged algorithm class. I've come to a point where I need separate threads for the processing and on-the-fly display of the results.

我有一个方法在我的非托管类做工作(我不认为我可以改变)。里面有一个主循环。我的计划是在每次迭代结束时启用绘制结果。

I have a single method in my unmanaged class that does the work(I don't think I can change that). Inside it there is a main loop. My plan was to enable drawing the results at the end of each iteration.

我想使用System :: Threading :: Monitor方法来执行同步。但是,它们需要一个托管引用来工作,我不能在非托管类中创建。我应该如何解决这个问题并执行线程同步?

I wanted to use System::Threading::Monitor methods to perform the synchronization. However they require a managed reference to work which I cannot create in an unmanaged class. How am I supposed to solve that problem and perform thread synchronization?

推荐答案

将类切换到托管,或使用非托管同步对象。如果由于某些原因无法更改要管理的算法,则可以有两个类 - 一个管理,另一个不使用算法管理。第一个将使用另一个的功能,并将使用Monitor为它提供同步。

Either switch your class to managed, or use unmanaged synchronization objects. If you for some reason cannot change the algorithm to be managed, you can have two classes - one managed, the other one unmanaged with algorithm. The first one will use functionality of the other one and will provide synchronization using Monitor for it.

或者,如果要保持整个代码不受管理,请转到Windows API同步。有关详细信息,请参阅MSDN函数列表 - 查看CreateMutex,CreateSemaphore和InitializeCriticalSection。 Mutex和临界区非常类似于Monitor类提供的简单锁。 (实际上,Monitor被实现以与它们相同的方式工作,为信令添加一些更多的功能。)关于信令的信息,参见CreateEvent。

Or, if you want to keep whole code unmanaged, go to Windows API for synchronization. See MSDN list of functions - look at CreateMutex, CreateSemaphore and InitializeCriticalSection for more information. Mutex and critical section are very similar to simple lock provided by Monitor class. (In fact, Monitor is implemented to work the same way as them, adding some more functionality for signalling.) See CreateEvent for information on signalling.

这篇关于C ++ / CLI线程同步,包括托管和非托管代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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