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:
31 virtual void enter ();
32 virtual bool try_enter ();
33 virtual void leave ();
34
35private:
36 CRITICAL_SECTION section;
37
38 // critical sections cannot be copied or assigned
39 criticalsection (const criticalsection&) = delete;
40 criticalsection& operator= (const criticalsection&) = delete;
41};
42
69
70class lock
71{
72public:
75
77 lock (const lock& t);
78
80 ~lock ();
81
82private:
83 criticalsection& section;
84};
85
88{
89 InitializeCriticalSection (&section);
90}
91
94{
95 DeleteCriticalSection (&section);
96}
97
100{
101 EnterCriticalSection (&section);
102}
103
106{
107 return (TryEnterCriticalSection (&section) != 0);
108}
109
112{
113 LeaveCriticalSection (&section);
114}
115
117 : section (cs)
118{
119 section.enter ();
120}
121
122inline lock::lock (const lock& t)
123 : section (t.section)
124{
125 section.enter ();
126}
127
128inline lock::~lock ()
129{
130 section.leave ();
131}
132
133} // namespace mlib
Lightweight inter-thread synchronization.
Definition critsect.h:27
virtual bool try_enter()
Return true if critical section was entered.
Definition critsect.h:105
~criticalsection()
Deletes the critical section object.
Definition critsect.h:93
criticalsection()
Initializes critical section object.
Definition critsect.h:87
virtual void enter()
Enter critical section.
Definition critsect.h:99
virtual void leave()
Leave critical section.
Definition critsect.h:111
lock(criticalsection &cs)
Acquire critical section.
Definition critsect.h:116
~lock()
Leave critical section.
Definition critsect.h:128