0

I had previously asked this question Removing Windows library dependencies about removing Windows library dependencies. User: https://stackoverflow.com/users/214671/matteo-italia had already explained that for Threads you can replace windows specific code with std::mutex but as for Processes there is currently nothing in the standard which is the answer that I had accepted.

In another good answer user: https://stackoverflow.com/users/7594711/soronelhaetir had suggested using boost-interprocess. However my current project is boost free.

With this current class:

BlockProcess:

BlockProcess.h

#ifndef BLOCK_PROCESS_H
#define BLOCK_PROCESS_H

#include <Windows.h>
#include <process.h>
#include <string>

namespace demo {

class BlockProcess final {
private:
    HANDLE hMutex_;
public:
    explicit BlockProcess( const std::string& strName );
    ~BlockProcess();

    bool isBlocked() const;

    BlockProcess( const BlockProcess& c ) = delete;
    BlockProcess& operator=( const BlockProcess& c ) = delete;
};

} // namespace demo

#endif // !BLOCK_PROCESS_H

BlockProcess.cpp

#include "BlockProcess.h"

namespace demo {

BlockProcess::BlockProcess( const std::string& strName ) {
    hMutex_ = CreateMutex( nullptr, FALSE, strName.c_str() );
} 

BlockProcess::~BlockProcess() {
    CloseHandle( hMutex_ );
}

bool BlockProcess::isBlocked() const {
    return (hMutex_ == nullptr || GetLastError() == ERROR_ALREADY_EXISTS);
}

} // namespace demo

My question is still partially the same: removing windows dependencies but now becomes: Is there a way that I can rewrite this class in a generic fashion so that I'm not inclined to have to use: #include <Windows.h> while not using any third party libraries such as boost?

Francis Cugler
  • 7,788
  • 2
  • 28
  • 59
  • Portability does not imply that library does not use platform-specific APIs or headers. It only implies that library implements a set of platform-independent features on top of it (potentially using different implementations on different platforms). – user7860670 Jan 25 '18 at 19:55
  • *However my current project is boost free* -- Is that supposed to be a good thing or a bad thing? – PaulMcKenzie Jan 25 '18 at 19:57
  • @VTT No; I understand that boost basically writes wrappers over top of multiple platform specific code to give a general appearance of being generic. I was just trying to make my `class` portable in a non-platform specific type context. – Francis Cugler Jan 25 '18 at 20:04
  • @PaulMcKenzie Neither to be honest. I'm not being opinionated for nor against boost. I particularly don't mind using boost, albeit this particular project is not using it. Was trying to aim away from having third party dependencies if possible. – Francis Cugler Jan 25 '18 at 20:06
  • Why not use the file system? Implementing a full-fledged mutex on top of the file system isn't trivial, but simple uses such as enforcing single instance can be achieved by trying to open some dummy file in exclusive mode - if failed, some other instance is already holding it. – Eran Jan 25 '18 at 20:40

1 Answers1

2

Unfortunately the current C++ standard library does not provide much in the way of IPC and does not include any named mutex support. (It provides shared_mutex but that's a different thing.) Therefore you're stuck with external dependencies. That external dependency can be the Windows API as you've done, or Boost (which provides a named_mutex), or something else.

I completely understand your reluctance to introduce a dependency on Boost. But if you try to handle this yourself and start to add alternative platform support, you'll quickly find that you're simply reinventing the Boost libraries and almost certainly not doing as good a job. So in this case you should weigh your requirements for portability against your distaste for biting the bullet and using Boost.

TypeIA
  • 16,916
  • 1
  • 38
  • 52
  • 1
    I completely understand, however I have also just came across this article `https://blogs.msdn.microsoft.com/oldnewthing/20060620-13/?p=30813/` and now it is making me think twice about having this kind of mechanism in the application... – Francis Cugler Jan 25 '18 at 20:02
  • 1
    @FrancisCugler The single-instance check is controversial, some do see it as an anti-pattern. See also https://stackoverflow.com/a/646505/1274314. If you don't really need it, maybe you can save yourself some trouble. – TypeIA Jan 25 '18 at 20:07
  • It isn't so much a `distaste` for `boost`. I don't particularly have anything against boost. It's just that this current project is not using boost and in general I was trying to alleviate the need for any `3rd` party library while keeping the code base with both portability and as generic - reusable as possible. – Francis Cugler Jan 25 '18 at 20:17
  • 1
    @FrancisCugler Maybe I chose the wrong word; I really do understand and have had the same struggle myself. I avoid Boost and other dependencies wherever possible and when not possible, part of me is sad and another part of me is glad Boost exists so I don't have to do it myself. – TypeIA Jan 25 '18 at 20:25