-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuniqueptrdeleter.cpp
46 lines (38 loc) · 1.07 KB
/
uniqueptrdeleter.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// {"category": "C++11", "notes": "unique_ptr deleter"}
#include <SDKDDKVer.h>
#include <stdio.h>
#include <tchar.h>
#include <iostream>
#include <memory>
#include <Windows.h>
using namespace std;
//------------------------------------------------------------------------------
//
// unique_ptr deleter
//
//------------------------------------------------------------------------------
struct HandleDeleter
{
void operator()(HANDLE handle) const
{
CloseHandle(handle);
}
};
using HandlePtr = unique_ptr<void, HandleDeleter>;
//------------------------------------------------------------------------------
//
// Demo execution
//
//------------------------------------------------------------------------------
int _tmain(int argc, _TCHAR* argv[])
{
DWORD msecSleep = 10000;
HandlePtr handle(CreateThread(nullptr, 0,
[](_In_ LPVOID lpParameter) -> DWORD
{
Sleep(*reinterpret_cast<LPDWORD>(lpParameter));
return ERROR_SUCCESS;
}, &msecSleep, 0, nullptr));
WaitForSingleObject(handle.get(), INFINITE);
return 0;
}