MLIB
Loading...
Searching...
No Matches
critsect.h
Go to the documentation of this file.
1/*
2 Copyright (c) Mircea Neacsu (2014-2025) Licensed under MIT License.
3 This file is part of MLIB project. See LICENSE file for full license terms.
4*/
5
6
8
9#pragma once
10
11#if __has_include("defs.h")
12#include "defs.h"
13#endif
14#include "safe_winsock.h"
15
16namespace mlib {
17
27{
28public:
30 virtual void enter ();
31 virtual bool try_enter ();
32 virtual void leave ();
33
34private:
35 std::shared_ptr<CRITICAL_SECTION> section;
36
37};
38
65
66class lock
67{
68public:
71
73 lock (const lock& t);
74
76 ~lock ();
77
78private:
79 criticalsection& section;
80};
81
84{
85 auto sect = new CRITICAL_SECTION;
86 InitializeCriticalSection (sect);
87 section = std::shared_ptr<CRITICAL_SECTION> (sect, DeleteCriticalSection);
88}
89
92{
93 EnterCriticalSection (section.get());
94}
95
98{
99 return (TryEnterCriticalSection (section.get()) != 0);
100}
101
104{
105 LeaveCriticalSection (section.get());
106}
107
109 : section (cs)
110{
111 section.enter ();
112}
113
114inline lock::lock (const lock& t)
115 : section (t.section)
116{
117 section.enter ();
118}
119
120inline lock::~lock ()
121{
122 section.leave ();
123}
124
125} // namespace mlib
Lightweight inter-thread synchronization.
Definition critsect.h:27
virtual bool try_enter()
Return true if critical section was entered.
Definition critsect.h:97
criticalsection()
Initializes critical section object.
Definition critsect.h:83
virtual void enter()
Enter critical section.
Definition critsect.h:91
virtual void leave()
Leave critical section.
Definition critsect.h:103
lock(criticalsection &cs)
Acquire critical section.
Definition critsect.h:108
~lock()
Leave critical section.
Definition critsect.h:120