-
Notifications
You must be signed in to change notification settings - Fork 239
Semaphore handles
Raymond Chen edited this page Aug 24, 2020
·
2 revisions
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);