11

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.

Mr. Boy
  • 60,845
  • 93
  • 320
  • 589
  • 1
    What features are missing that you require? If the answer to this is none, I don't see why you need to worry about "criticism" from other folks who may not share your requirements... – Nim Feb 28 '11 at 10:05
  • 1
    What exactly are they criticising? Can you provide links? Like it is, the question is too vague – BЈовић Feb 28 '11 at 10:12
  • 2
    It's not the features but the implementation that was criticised. I don't see how the question is vague... if there are issues with Boost's implementation then share them, if there are better libraries, list them. – Mr. Boy Feb 28 '11 at 10:27
  • This is the first time I hear for such issues, but then my windows experience is very limited. Your initial question asked about issues with boost IPC. The question, as it is, looks more like a rant. Can you provide links or an example that confirm these issues? – BЈовић Feb 28 '11 at 11:10
  • 1
    Of course I can't. That's the whole _point_ of the question, to see if these claimed issue are valid. – Mr. Boy Feb 28 '11 at 12:20
  • @John: Why not just check the sources, then you'll know for sure. :) – Jörgen Sigvardsson Mar 01 '11 at 11:18
  • Because I don't know much about IPC and I have better things to do with my time, when some expert probably already knows the answer. – Mr. Boy Mar 01 '11 at 11:58
  • 1
    It's sad this question was closed as it is a good question. Boost is designed to be portable between Windows and Linux. There are areas where this is problematic because of the differince between the two systems, with shared memory being a prime example. Windows has very little features in the shared memory department, so many of boosts IPC features are emulated within userspace on Windows because they're simply unavailable at a lower level. You might have asked the question better, but its still an important differince to understand when using boost on windows. – JSON Dec 11 '13 at 12:57

2 Answers2

10

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).

Ze Blob
  • 2,817
  • 22
  • 16
  • 1
    Hmm, well lots of things work differently on different OS, that's why we _need_ libraries like boost, etc. I was under the impression Linux had it's own more efficient mechanisms, like Windows does, so I'd expect a cross-platform library to use the per-platform efficient mechanisms and provide me a consistent abstraction. – Mr. Boy Mar 01 '11 at 08:25
  • @John The problem with that is that you'd reduce the portability while increasing the size and complexity of your library. If you have to re-write a large portion of your library for every OS you want to support, you're probably doing it wrong. Mind you, this is pure speculation. You could always try asking on the Boost mailing list. – Ze Blob Mar 01 '11 at 16:36
  • 3
    The whole point of a cross-platform library is it wraps the low-level things which _are_ different on various platforms with a common abstraction. The fact those low-level things are different is the whole point _of_ the library... look at cross-platform GUI libraries for example. – Mr. Boy Mar 02 '11 at 16:03
  • @John Did some more digging so see the updated answer. – Ze Blob Mar 02 '11 at 19:19
  • Good info. I guess I had always thought of boost as a finished product like STL. – Mr. Boy Mar 02 '11 at 20:11
  • Another comment I came across from someone using cross-process rendering (on Mac): "Boost interprocess seems to be weak around its usage of interprocess mutexes: they don't get cleaned up if a process crashes" – Mr. Boy Apr 05 '11 at 08:08
  • @John That's true and that's why it's a good idea to call `remove` before creating the mutex. That being said, in some programs it can be difficult to determine whether an existing mutex is live or a left-over from a crash. I'm not sure whether there's a simple solution to this problem. – Ze Blob Apr 05 '11 at 22:08
  • @ZeBlob See the [release notes for Boost 1.76](https://www.boost.org/doc/libs/1_77_0/doc/html/interprocess/acknowledgements_notes.html#interprocess.acknowledgements_notes.release_notes). The native WinAPI implementation can be forced by using `BOOST_INTERPROCESS_FORCE_NATIVE_EMULATION`. – Bryce Schober Nov 16 '21 at 16:28
3

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.

yasouser
  • 5,113
  • 2
  • 27
  • 41