Skip to content

Instantly share code, notes, and snippets.

View drodil's full-sized avatar
💣

Heikki Hellgren drodil

💣
View GitHub Profile
@drodil
drodil / car_door_v1.hpp
Last active May 30, 2018 20:53
Copy constructor derp
#include <string>
class CarDoor {
public:
explicit CarDoor(const std::string& name) : mName(name) { }
~CarDoor() = default;
// Let's imagine a lot of functions here
@drodil
drodil / wearos_top
Created September 18, 2018 04:46
WearOS top
Tasks: 310 total, 5 running, 302 sleeping, 0 stopped, 1 zombie
Mem: 468960k total, 459408k used, 9552k free, 2300k buffers
Swap: 262140k total, 118768k used, 143372k free, 178380k cached
400%cpu 5%user 6%nice 28%sys 359%idle 2%iow 0%irq 1%sirq 0%host
PID USER PR NI VIRT RES SHR S[%CPU] %MEM TIME+ ARGS
515 system 18 -2 867M 46M 32M S 27.1 10.1 4:35.40 system_server
4861 shell 20 0 5.7M 1.8M 1.1M R 5.3 0.3 0:03.87 top
350 system 20 0 8.0M 760K 592K S 1.5 0.1 0:01.94 [email protected]
@drodil
drodil / thread_id.cpp
Last active September 28, 2018 08:12
C++ human-readable thread id
#include <mutex>
#include <thread>
#include <unordered_map>
std::size_t get_thread_id() noexcept {
static std::size_t thread_idx = 0;
static std::mutex thread_mutex;
static std::unordered_map<std::thread::id, std::size_t> thread_ids;
std::lock_guard<std::mutex> lock(thread_mutex);
@drodil
drodil / thread_id2.cpp
Last active January 17, 2019 16:27
C++ human-readable thread id
#include <thread>
#include <atomic>
std::size_t get_thread_id() noexcept {
static std::atomic<std::size_t> thread_idx{0};
thread_local std::size_t id = thread_idx;
thread_idx++;
return id;
}
#include <iostream>
#include <bitset>
#include <type_traits>
namespace test {
// Allows ~ operator for any enum class in the same namespace
template<class Enum, class = typename std::enable_if<std::is_enum<Enum>::value, Enum>::type>
inline Enum operator~(Enum a) {
return static_cast<Enum>(~static_cast<typename std::underlying_type<Enum>::type>(a));
}
#include <iostream>
class BaseOutputter {
public:
static void set_prefix_message(const std::string& msg) {
prefix_message = msg;
}
protected:
BaseOutputter() = default;
// test_class.hpp
#include <iosfwd>
class TestClass {
public:
TestClass() = default;
void hello_to_stream(std::stringstream& ss) const noexcept;
};
#include <iostream>
class BaseClass {
public:
void say_hello() {
std::cout << "Hello";
}
protected:
void say_world() {
std::cout << "world!";
T0 increasing counter
T1 increasing counter
==================
WARNING: ThreadSanitizer: data race (pid=4888)
Write of size 8 at 0x0000011b4980 by thread T2:
#0 foo() ??:? (a.out+0x4c521b)
#1 void std::_Bind_simple<void (*())()>::_M_invoke<>(std::_Index_tuple<>) ??:? (a.out+0x4cbeb5)
#2 std::_Bind_simple<void (*())()>::operator()() ??:? (a.out+0x4cbe48)
#3 std::thread::_Impl<std::_Bind_simple<void (*())()> >::_M_run() ??:? (a.out+0x4cbaac)
#4 std::this_thread::__sleep_for(std::chrono::duration<long, std::ratio<1l, 1l> >, std::chrono::duration<long, std::ratio<1l, 1000000000l> >) ??:? (libstdc++.so.6+0xb8c7f)
#include <vector>
std::size_t counter = 0;
std::mutex cout_mutex;
void foo()
{
{
std::lock_guard<std::mutex> lock(cout_mutex);
std::cout << "T" << get_thread_id() << " increasing counter" << std::endl;
}