Removing Windows library dependencies

浪子不回头ぞ 提交于 2020-01-06 06:46:49

问题


I have a couple of classes here that I would like to remove window library dependencies for portability reasons. One for blocking processes and the other for blocking threads. Both of these classes compile & runs fine as is... As for the BlockProcess class it is currently using a HANDLE for a mutex and using function calls such as: {CreateMutex(), CloseHandle() & GetLastError()}. As for the BlockThread class it uses a pointer to a CRITICAL_SECTION calling functions such as: {EnterCricticalSection() & LeaveCriticalSection()}.

What I would like to know is there an equivalent way to do this using something from the std library such as std::mutex, std::thread etc. that will provide the same functionality. I would like to remove the need to have #include<windows.h> & possibly #include<process.h>.

Here are my existing classes:


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);
}

BlockThread.h

#ifndef BLOCK_THREAD_H
#define BLOCK_THREAD_H

#include <Windows.h>

namespace demo { 

class BlockThread final {
private:
    CRITICAL_SECTION* pCriticalSection_;

public:
    explicit BlockThread( CRITICAL_SECTION& criticalSection );
    ~BlockThread();

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

} // namespace demo

#endif // !BLOCK_THREAD_H


} // namespace demo

BlockThread.cpp

#include "BlockThread.h"

namespace demo { 

BlockThread::BlockThread( CRITICAL_SECTION& criticalSection ) {
    pCriticalSection_ = &criticalSection;
    EnterCriticalSection( pCriticalSection_ );
} 

BlockThread::~BlockThread() {
    LeaveCriticalSection( pCriticalSection_ );
}

} // namespace demo

Edit

If you need an example of how these classes are used feel free to leave a comment.


回答1:


Your BlockThread is essentially std::lock_guard (to be used over an std::mutex instead of CRITICAL_SECTION). It can be trivially replaced with standard library code.

As for BlockProcess (creation of a cross-process synchronization object), there's no standard equivalent.




回答2:


boost interprocess does offer cross-platform inter-process synchronization objects (akin to the inter-thread objects of the standard library). But the standard library types are purely in-process.



来源:https://stackoverflow.com/questions/48450045/removing-windows-library-dependencies

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!