-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnt4-exe-test.cpp
56 lines (44 loc) · 1.19 KB
/
nt4-exe-test.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
47
48
49
50
51
52
53
54
55
56
#include <Windows.h>
#include <assert.h>
#include <atomic>
#include <exception>
#include <list>
#include <mutex>
#include <stdlib.h>
#include <string>
#include <thread>
#define NUM_THREADS 4
#define COUNT_PER_THREAD 1000000
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
std::list<std::thread> threads;
std::atomic<int> i_counter(0);
std::string s_counter = "0";
std::mutex s_mutex;
for(int j = 0; j < NUM_THREADS; ++j)
{
int thread_id = j;
threads.emplace_back([&i_counter, &s_counter, &s_mutex, thread_id]()
{
try {
for(int i = 0; i < COUNT_PER_THREAD; ++i)
{
++i_counter;
std::lock_guard<std::mutex> lock(s_mutex);
s_counter = std::to_string(atoi(s_counter.c_str()) + 1);
}
throw std::runtime_error("thread " + std::to_string(thread_id));
}
catch(...) {}
});
}
while(!threads.empty())
{
threads.front().join();
threads.pop_front();
}
assert(i_counter == (NUM_THREADS * COUNT_PER_THREAD));
assert(s_counter == std::to_string(NUM_THREADS * COUNT_PER_THREAD));
MessageBox(NULL, L"Hello from 2024!", L"Hello", MB_OK);
return 0;
}