Skip to content

Instantly share code, notes, and snippets.

View kohakus's full-sized avatar
🏖️

Yiteng Zhang kohakus

🏖️
View GitHub Profile
@kohakus
kohakus / singleton.h
Last active June 17, 2020 13:46
Singleton and Thread Safe.
/* Some Singleton Implementation Examples. */
/* Mainly consider of lazy initialization. */
// Version 1: simple lazy singleton.
// Not thread safe. Can only be used in single thread.
template<typename T>
class Singleton {
public:
static T& getInstance() {
@kohakus
kohakus / endian.cc
Created June 16, 2020 13:53
check big-endian/little-endian of host.
#include <iostream>
#include <cinttypes>
int main (void) {
uint32_t num = 0x04030201;
unsigned char* cp = reinterpret_cast<unsigned char*>(&num);
if (*cp == 1) {
std::cout << "littile-endian" << std::endl;
} else if (*cp == 4) {
@kohakus
kohakus / rust_references.rs
Created March 14, 2020 19:37
Rust multiple immutable/mutable references
// Reference Rules for a variable
// a). At any given time, you can have either (but not both of) one mutable reference or any number of immutable references.
// b). References must always be valid.
// First we define a simple struct
struct Foo<T> {
data: T,
}
impl<T> Foo<T> {