mirror of
https://github.com/Artikash/Textractor.git
synced 2024-12-23 17:04:12 +08:00
more efficient thread safe wrapper
This commit is contained in:
parent
4ea5c9cb32
commit
afea96e9d5
@ -53,8 +53,8 @@ namespace
|
||||
WinMutex viewMutex;
|
||||
};
|
||||
|
||||
ThreadSafePtr<std::unordered_map<ThreadParam, std::shared_ptr<TextThread>>> textThreadsByParams;
|
||||
ThreadSafePtr<std::unordered_map<DWORD, ProcessRecord>> processRecordsByIds;
|
||||
ThreadSafe<std::unordered_map<ThreadParam, std::shared_ptr<TextThread>>> textThreadsByParams;
|
||||
ThreadSafe<std::unordered_map<DWORD, ProcessRecord>> processRecordsByIds;
|
||||
|
||||
ThreadParam CONSOLE{ 0, -1ULL, -1ULL, -1ULL }, CLIPBOARD{ 0, 0, -1ULL, -1ULL };
|
||||
|
||||
|
@ -22,7 +22,7 @@ public:
|
||||
void AddSentence(std::wstring sentence);
|
||||
void Push(const BYTE* data, int len);
|
||||
|
||||
const ThreadSafePtr<std::wstring> storage;
|
||||
ThreadSafe<std::wstring> storage;
|
||||
const int64_t handle;
|
||||
const std::wstring name;
|
||||
const ThreadParam tp;
|
||||
|
@ -5,25 +5,25 @@
|
||||
|
||||
template <typename T> using Array = T[];
|
||||
|
||||
template<typename E, typename M = std::mutex, template<typename...> typename P = std::unique_ptr>
|
||||
class ThreadSafePtr
|
||||
template<typename E, typename M = std::mutex>
|
||||
class ThreadSafe
|
||||
{
|
||||
public:
|
||||
template <typename ...Args> ThreadSafePtr(Args ...args) : ptr(new E(args...)), mtxPtr(new M) {}
|
||||
auto operator->() const
|
||||
template <typename ...Args> ThreadSafe(Args ...args) : contents(args...) {}
|
||||
auto operator->()
|
||||
{
|
||||
struct
|
||||
{
|
||||
E* operator->() { return ptr; }
|
||||
std::unique_lock<M> lock;
|
||||
E* ptr;
|
||||
} lockedProxy{ std::unique_lock<M>(*mtxPtr), ptr.get() };
|
||||
} lockedProxy{ std::unique_lock(mtx), &contents };
|
||||
return lockedProxy;
|
||||
}
|
||||
|
||||
private:
|
||||
P<E> ptr;
|
||||
P<M> mtxPtr;
|
||||
E contents;
|
||||
M mtx;
|
||||
};
|
||||
|
||||
struct DefHandleCloser { void operator()(void* h) { CloseHandle(h); } };
|
||||
|
Loading…
x
Reference in New Issue
Block a user