跨越C API边界传递句柄C ++类 [英] Passing handle to C++ classes across a C API boundary

查看:216
本文介绍了跨越C API边界传递句柄C ++类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写在C ++库,但希望它有一个C API,这也应该是线程安全的。该API需要做的一件事就是来回传递把手库中创建的对象(例如包含引用或指针的结构)。这些对象需要在某一时刻被破坏,因此,任何手柄到这样一个对象,它仍然存在将它们变为无效。

I am writing a library in C++, but want it to have a C API, which should also be thread safe. One thing that the API needs to do is to pass back and forth handles (e.g. a structure containing a reference or pointer) of objects created within the library. These objects need to be destroyed at some point, so any handles to such an object that were still in existence would them become invalid.

编辑:我们不能假定每个手柄只有一个单一的客户端线程中使用。我特别想处理这种情况有两个客户端线程同时访问同一资源的情况下,人们试图摧毁它,而其他试图修改

We cannot assume that each handle is only used within a single client thread. In particular I want to handle the case where there are two client threads accessing the same resource simultaneously, one trying to destroy it while the other tries to modify it

有两种模式来处理这个问题。一个是智能指针模式,例如升压:: shared_ptr的或std :: shared_ptr的,这确保当有以不再引用的对象只销毁。不过,据我所知,这样的指针是不可能用C来实现(因为它不支持构造函数和析构函数),所以这种方式就不会在我的情况下工作。我不想依靠用户呼吁他们获取句柄的每个实例的释放的功能,因为他们难免不会。

There are two paradigms for dealing with this. One is the smart pointer paradigm, for example boost::shared_ptr or std::shared_ptr, which make sure the object is only destroyed when there are no more references to it. However, as I understand it, such pointers aren't possible to implement in C (as it doesn't support constructors and destructors), so this approach won't work in my case. I don't want to rely on the user to call a "release" function for every instance of a handle they obtain, since they inevitably won't.

另一个范例是简单地摧毁库中的对象,并有后续的函数调用它传递一个句柄对象作为输入简单的返回一个错误code。我的问题是什么样的技术或库可用来实现这种方法,特别是在多线程应用程序?

Another paradigm is to simply destroy the object within the library, and have any subsequent function calls which pass a handle to that object as an input simply return an error code. My question is what techniques or libraries are available to implement this approach, specifically in a multi-threaded application?

编辑:
当然,图书馆应处理内存本身释放的所有配置。智能指针也可以使用库中;他们可能不能跨越API传递,但是。

The library should of course handle all the allocation of deallocation of internal memory itself. Smart pointers may be also used within the library; they may not be passed across the API, however.

编辑:
上把手更多细节可能在客户端一起使用:在客户端可能具有两个线程,其中之一创建对象。该线程可能手柄传递给第二线程,或第二线程可能使用一个find_object函数的库得到它。然后第二个线程可能会不断更新对象,但而会在第一线程可能会破坏对象,使所有句柄的对象无效。

More detail on handles might be used on the client side: the client might have two threads, one of which creates an object. That thread might pass the handle to the second thread, or the second thread might get it from the library using a "find_object" function. The second thread might then continuously update the object, but while that is going on the first thread might destroy the object, making all handles to the object invalid.

我AP preciate办法的粗糙的建议 - 我想出了其中的一些自己了。然而,事情的细节问题细节,如C ++类实例是如何给出一个手柄,而其他线程可能试图销毁的对象,是不平凡的,所以我喜欢我的答案后,我真的检索和锁定,做了以下和它的作品没有失败。或者,甚至更好,此库实现您明智而安全地后,在做什么。

I appreciate rough suggestions of an approach - I have come up with some of these myself too. However, the nitty gritty details of things like how a C++ class instance is retrieved and locked, given a handle, while other threads may be attempting to destroy that object, are non-trivial, so I'm really after answers like "I have done the following and it works without fail." or, even better, "This library implements what you're after sensibly and safely".

推荐答案

这在地图智能指针保持作为键值内部可能是一个可行的解决方案恕我直言手柄(例如简单的整数)。

IMHO handles (e.g. simple integer numbers) that are kept as key values in a map of smart pointers internally might be a viable solution.

不过你得有这种保证是由特定客户端释放不会破坏映射条目把手的机制,只要把手在多线程环境中仍然使用其他客户端。

Though you'll need to have a mechanism that guarantees a handle that was freed by a particular client won't destroy the map entry, as long the handle is still used by other clients in a multithreaded environment.

更新:结果
至少@ user1610015答案不是一个坏主意(如果你有一个COM启用环境中工作)。在任何情况下,你需要跟踪你的内部管理类实例的一些引用计数。

UPDATE:
At least @user1610015 answer isn't such a bad idea (if you're working with a COM enabled environment). In any case you'll need to keep track with some reference counting of your internally managed class instances.

我不这么用C ++ 11经历过或提高智能指针功能,以及它是如何可能的拦截或覆盖这些引用计数机制。但如 Alexandrescou的洛基库的智能指针注意事项可以让你实现你打算如何处理引用计数适当的政策和哪些接口可以访问此与否。

I'm not so experienced with the C++11 or boosts smart pointer features, and how it's possible to intercept or override reference counting mechanism with these. But e.g. Alexandrescou's loki library smart pointer considerations may let you implement an appropriate policy on how you're going to handle the reference counting and which interfaces can access this or not.

这应该是可能的Alexandrescou的智能指针,以提供一个参考计数,支持通过C-API接入和C ++内部实现同时机制和线程安全的。

It should be possible with Alexandrescou's Smart Pointer to provide a ref counting mechanism that supports access via a C-API and C++ internal implementation concurrently and thread safe.

这篇关于跨越C API边界传递句柄C ++类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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