Created
July 17, 2014 06:42
-
-
Save goldshtn/db3098eca2fc2d345f4c to your computer and use it in GitHub Desktop.
Demo app for experimenting with WinDbg. Compile with Visual C++ 2013.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Windows.h> | |
#include <vector> | |
#include <iostream> | |
#include <thread> | |
#include <random> | |
class DynamicArray | |
{ | |
char* data_; | |
unsigned len_; | |
public: | |
DynamicArray(unsigned const len) : data_{ new char[len] }, len_{ len } | |
{ | |
} | |
char& operator[](unsigned index) | |
{ | |
return data_[index]; | |
} | |
~DynamicArray() | |
{ | |
delete[] data_; | |
} | |
}; | |
void AllocatingThread() | |
{ | |
std::default_random_engine engine; | |
std::uniform_int_distribution<unsigned> distribution(10, 1000); | |
for (;;) | |
{ | |
unsigned size = distribution(engine); | |
DynamicArray arr{ size }; | |
arr[5] = 'c'; | |
std::this_thread::sleep_for(std::chrono::milliseconds(50)); | |
} | |
} | |
HANDLE g_lock; | |
HANDLE g_mainLock; | |
void MutexThread() | |
{ | |
std::cout << "Inside mutex thread, trying to lock both mutexes." << std::endl; | |
HANDLE mutexes[] { g_lock, g_mainLock }; | |
WaitForMultipleObjects(ARRAYSIZE(mutexes), mutexes, TRUE, INFINITE); | |
ReleaseMutex(g_lock); | |
ReleaseMutex(g_mainLock); | |
} | |
int main() | |
{ | |
g_lock = CreateMutex(nullptr, FALSE, L"Mutex for Mutex Thread"); | |
g_mainLock = CreateMutex(nullptr, TRUE /*initial owner*/, L"Mutex for Main Thread"); | |
std::thread allocatingThread{ AllocatingThread }; | |
std::cout << "Inside main thread, starting wait on mutex thread." << std::endl; | |
std::thread mutexThread{ MutexThread }; | |
mutexThread.join(); | |
ReleaseMutex(g_mainLock); | |
allocatingThread.join(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment