Boost IPC好用吗?

发布于 2024-10-19 22:44:19 字数 357 浏览 3 评论 0原文

我对跨平台 IPC 的默认选择是 boost,但当我询问它时,我看到它在两个不同的论坛上受到批评,这让我很担心。也许这只是一个巧合,那么对于 boost IPC 和选择跨平台 C++ IPC 库的总体想法是什么?

对于 Windows 开发,我们使用 VC++ 2008 作为参考。

编辑:这是我见过的评论的示例(现在找不到它们):

对于提升来说,这是垃圾。至少在 视窗。互斥锁不使用 WinAPI, 相反,他们创造了自己的 基于文件的实现 (WinAPI = 内核对象)。如果你的程序 崩溃文件不会被删除。 下次启动您的程序时 无法创建互斥体,因为 现有文件。

My default choice for cross-platform IPC would be boost, but I saw it criticised in two different forums when I asked about it and this concerned me. Perhaps this was simply a coincidence, so what are the thoughts on boost IPC and choosing a cross-platform C++ IPC library in general?

For Windows dev we're using VC++ 2008 for reference.

edit: here is an example of comments I've seen made (can't find them all right now):

for boost, it's crap. At least on
windows. The Mutexes don't use WinAPI,
instead they create it's own
File-Based implementation (WinAPI =
Kernel-Objects). If your Program
crashes the files won't be deleted.
Next time your Programm is launched
the mutex can't be created, because of
the existing file.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无人问我粥可暖 2024-10-26 22:44:19

根据我对 Boost.Interprocess 的有限经验,我没有遇到任何重大问题,但我无法真正评论性能。虽然它确实使用在程序文件夹之外创建的文件来完成其工作,但它们都应该是内存映射的,这可以消除大多数性能问题。无论如何,当您使用 IPC 时,您总是应该预料到一些额外的性能开销。

至于您强调的​​批评,可以删除先前进程留下的命名互斥体或任何其他命名资源(请参阅静态 删除(const char*)函数)。公平地说,根据您的应用程序,正确使用可能会很棘手,但这不是您在使用 Windows API 时必须处理的问题。另一方面,Windows API 不可移植。我的猜测是,他们使用文件(即使存在更好的替代方案)来保持库的界面和行为在不同平台上保持一致。

无论如何,命名互斥体只是该库提供的一小部分。更有用的功能之一是它提供了 共享内存区域的自己的内存管理器,其中包括STL 分配器。我个人发现使用它提供的高级结构比使用原始内存更令人愉快。


更新:我在 Boost 文档中做了更多的挖掘,发现了各种有趣的小窍门:

此页面提供了有关库的实现和性能的更多详细信息,但不包括实现原理。

此链接 明确指出他们的 Windows 互斥实现可能需要一些工作(版本 1.46)。如果您在 boost\interprocess\sync 文件夹中进一步挖掘,您会发现另外两个名为 posixemulation 的文件夹。它们都包含同步原语的实现细节。 interprocess_mutex::lock 的 posix 实现正是您所期望的,但模拟实现非常基本:

// Taken from Boost 1.40.
inline void interprocess_mutex::lock(void)
{
   do{
      boost::uint32_t prev_s = detail::atomic_cas32(const_cast<boost::uint32_t*>(&m_s), 1, 0);

      if (m_s == 1 && prev_s == 0){
            break;
      }
      // relinquish current timeslice
      detail::thread_yield();
   }while (true);
}

因此从外观上看,他们的目标是 Posix 支持,并将其他所有内容都放入模拟层中使用内存映射文件。如果您想知道为什么他们不提供专门的 Windows 实现,那么我建议询问 Boost 邮件列表(我在文档中找不到任何内容)。

From my limited experience with Boost.Interprocess, I didn't have any major problems but I can't really comment on performance. While it's true that it does use files created outside of your program's folder to do its stuff, they should all be memory mapped which negates most of the performance problems. In any case, when you're using IPC you should always expect some extra performance overhead.

As for the criticism you highlighted, it is possible to remove a named mutex or any other named resources that has been left lying around by a previous process (see the static remove(const char*) function). To be fair, depending on your application, it might be tricky to use correctly which is not something you have to deal with when using the Windows API. On the other hand, the Windows API isn't portable. My guess is that they use files (even when better alternative exists) to keep the interface and behaviour of the library consistent across different platforms.

Anyway, named mutexes is only small part of what the library provides. One of the more useful features is that it provides its own memory managers for the shared memory region which includes STL allocators. I personally find it more pleasant to work with the high level constructs it provides then with raw memory.


UPDATE: I did some more digging the in the Boost documentation and I came across various interesting tid bits:

This page gives a bit more detail about the implementation and the performances of the library but doesn't include an implementation rationale.

This link clearly states that their Windows mutex implementation could use some work (version 1.46). If you dig a little further in the boost\interprocess\sync folder, you'll notice two more folders named posix and emulation. Both of these contains the implementation details for the synchronisation primitive. The posix implementation for interprocess_mutex::lock is what you'd expect but the emulation implementation is pretty basic:

// Taken from Boost 1.40.
inline void interprocess_mutex::lock(void)
{
   do{
      boost::uint32_t prev_s = detail::atomic_cas32(const_cast<boost::uint32_t*>(&m_s), 1, 0);

      if (m_s == 1 && prev_s == 0){
            break;
      }
      // relinquish current timeslice
      detail::thread_yield();
   }while (true);
}

So by the looks of it, they aimed for Posix support and blobbed everything else into an emulation layer which uses memory mapped files. If you want to know why they don't provide a specialised Windows implementation then I suggest asking the Boost mailing list (I couldn't find anything in the doc).

陪你搞怪i 2024-10-26 22:44:19

这不是对您问题的直接答案,而是另一种选择:如果您可以选择开发环境,那么您可以考虑 Qt 和 Qt 中的 D-bus 实现

This is not a direct answer to your question, but an alternative: If you have a choice on the dev environment then you can consider Qt and the D-bus implementation in Qt.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文