Skip to content

Instantly share code, notes, and snippets.

// 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...
#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();
void lockMutexBad(bool shouldThrow) {
std::cout << "Locking mutex manually..." << std::endl;
globalMutex.lock();
std::cout << "Mutex is locked!" << std::endl;
// Could also imagine this as an early return. If you use a plain old
// mutex, you have to remember to manually unlock before every return...
// and it's quite easy to forget.
if (shouldThrow) {
std::cout << "Throwing exception" << std::endl;
void lockMutexGood(bool shouldThrow) {
std::cout << "Locking mutex with scoped_lock..." << std::endl;
std::scoped_lock lock(globalMutex);
std::cout << "Mutex is locked!" << std::endl;
if (shouldThrow) {
std::cout << "Throwing exception" << std::endl;
throw 1;
}
}
class CustomScopedLock {
public:
CustomScopedLock(std::mutex &m) : m_(m) {
std::cout << "CustomScopedLock locking mutex..." << std::endl;
m_.lock();
std::cout << "CustomScopedLock has locked mutex!" << std::endl;
};
~CustomScopedLock() {
m_.unlock();
template <typename Function>
class RaiiThread {
public:
RaiiThread(Function &&f) : t_(f){};
~RaiiThread() {
std::cout << "Joining thread in ~RaiiThread" << std::endl;
t_.join();
};
##### STARTUP
echo "hello mlim"
RPS1="${${KEYMAP/vicmd/-- NORMAL --}/(main|viins)/-- INSERT --}"
RPS2=$RPS1
##### BINDINGS
bindkey "^R" history-incremental-search-backward
bindkey "\e[A" history-beginning-search-backward
bindkey "\e[B" history-beginning-search-forward
set editing-mode vi
$if mode=vi
set keymap vi-command
Control-l: clear-screen
set keymap vi-insert
Control-l: clear-screen
$endif
##### History stuff
# Avoid consecutive duplicates. See https://www.gnu.org/software/bash/manual/html_node/Bash-Variables.html
export HISTCONTROL=ignoredups
# When the shell exits, append to the history file instead of overwriting it
shopt -s histappend
# See https://unix.stackexchange.com/questions/18212/bash-history-ignoredups-and-erasedups-setting-conflict-with-common-history
export PROMPT_COMMAND="history -n; history -w; history -c; history -r; $PROMPT_COMMAND"
# Unlimited history size
export HISTFILESIZE=
export HISTSIZE=
##### Protobuf Rules for Bazel
##### See https://github.com/bazelbuild/rules_proto
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
name = "rules_proto",
sha256 = "602e7161d9195e50246177e7c55b2f39950a9cf7366f74ed5f22fd45750cd208",
strip_prefix = "rules_proto-97d8af4dc474595af3900dd85cb3a29ad28cc313",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/rules_proto/archive/97d8af4dc474595af3900dd85cb3a29ad28cc313.tar.gz",