Страница:
Semaphore handles
Страницы
Ad hoc range helpers
Bitwise operation helpers
CppWinRT authoring helpers
Error handling customization
Error handling helpers internals
Error handling helpers
Error logging and observation
Event handles
Exception policy
File System Helpers (filesystem.h)
Home
Internals Local data
Kernel events
Kernel spin locks
Last error preservation
Lock guard object
Macro meta programming
Mutex handles
Parameter handling helpers
RAII resource wrapper details
RAII resource wrappers
RPC helpers
Re entrant locks
Reader writer locks
Registry Helpers
Secure data erasure
Semaphore handles
Shutdown aware objects
Slim events
String helpers
Template metaprogramming helpers
Token Helpers
Type validation helpers
WDF resource classes
WIL and CppWinRT together
Waiting on kernel objects
Win32 helpers
WinRT and COM wrappers internals
WinRT and COM wrappers
Windowing helpers
Windows Runtime helpers
safe_cast
string_maker
2
Semaphore handles
Raymond Chen редактировал(а) эту страницу 2020-08-24 08:18:11 -07:00
Содержание
The unique_semaphore
semaphore handle wrapper is defined in wil/resource.h
as part of the RAII resource wrappers library.
It is a
unique_any
specialization that adds methods specific to
the typical needs of dealing with a semaphore. There are three variants
that can be chosen based upon the error handling style the calling code
desires.
wil::unique_semaphore s1; // failures throw
wil::unique_semaphore_nothrow s2; // failures return HRESULTs
wil::unique_semaphore_failfast s3; // failures terminate
// Create during construction (not allowed with unique_semaphore_nothrow)
wil::unique_semaphore s4(0, 4, L"MySemaphoreName");
// Standard operations
s1.ReleaseSemaphore();
// Standard operations when going out of scope:
auto releaseOnExit = s1.ReleaseSemaphore_scope_exit();
// Acquire semaphore and release when going out of scope:
auto releaseOnExit = s1.acquire();
// Create or Open a Semaphore
s1.create(0, 2);
s1.open(L"MySemaphoreName");
if (s1.try_create(0, 4, L"MySemaphoreName")) {}
if (s1.try_open(L"MySemaphoreName")) {}
When dealing with semaphore handles outside of unique_semaphore
, WIL
defines some simple methods to emulate the same RAII patterns:
// Release the semaphore when the returned value goes out of scope
auto releaseOnExit = wil::ReleaseSemaphore_scope_exit(handle);