Skip to content

Instantly share code, notes, and snippets.

#include <iostream>
#include <mutex>
std::mutex globalMutex;
class CustomScopedLock {
public:
CustomScopedLock(std::mutex &m) : m_(m) {
std::cout << "CustomScopedLock locking mutex..." << std::endl;
m_.lock();
// Problem #1
{
int *arr = new int[dynamicSize];
} // arr goes out of scope but we didn't delete it, we have a memory leak
// Problem #2
std::mutex globalMutex;
void funcCalledInMultipleThreads() {
globalMutex.lock();
// Code that runs in multiple threads...