Skip to content

Instantly share code, notes, and snippets.

@goldshtn
Created July 17, 2014 06:42
Show Gist options
  • Save goldshtn/db3098eca2fc2d345f4c to your computer and use it in GitHub Desktop.
Save goldshtn/db3098eca2fc2d345f4c to your computer and use it in GitHub Desktop.
Demo app for experimenting with WinDbg. Compile with Visual C++ 2013.
#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