Skip to content

Instantly share code, notes, and snippets.

@yohhoy
yohhoy / fizzbuzz.cpp
Created August 16, 2012 13:31
FizzBuzz stream
#include <ostream>
#include <string>
#include <locale>
// FizzBuzz streambuf
template <class CharT, class Traits = std::char_traits<CharT> >
class basic_fizzbuzz_streambuf : public std::basic_streambuf<CharT, Traits> {
typedef std::basic_streambuf<CharT, Traits> sbuf_type;
typedef typename sbuf_type::traits_type traits_type;
typedef typename sbuf_type::char_type char_type;
@yohhoy
yohhoy / rmutex.cpp
Created August 20, 2012 09:37
recursive_mutex
/*
* Hand-coded recursive_mutex classes
*
* (C) Copyright yohhoy 2012.
* Distributed under the Boost Software License, Version 1.0.
* (See copy at http://www.boost.org/LICENSE_1_0.txt)
*/
#include <thread>
#include <mutex>
#include <condition_variable>
@yohhoy
yohhoy / cps_fib.cpp
Created November 9, 2012 02:40
C++ CPS
#include <iostream>
int fib(int n)
{
if (n <= 2)
return 1;
else
return fib(n - 1)
+ fib(n - 2);
}
@yohhoy
yohhoy / bitmask_type.cpp
Last active December 12, 2015 06:38
snippet for Bitmask type(17.5.2.1.3 [bitmask.types])
#include <type_traits>
// 17.5.2.1.3 Bitmask types
enum class BitmaskType : unsigned int {
Field1 = 0x00000001,
Field2 = 0x00000002,
Field3 = 0x00000004,
Field4 = 0x00000008,
Field5 = 0x00000010,
//...
@yohhoy
yohhoy / naive_fair_mutex.cpp
Last active December 19, 2015 19:58
naive fair mutex implementation (with recursive semantics)
#include <cassert>
#include <queue>
#include <condition_variable>
#include <mutex>
#include <thread>
class fair_recursive_mutex {
std::mutex mtx_;
std::condition_variable cv_;
std::queue<std::thread::id> q_;
@yohhoy
yohhoy / ticket_fair_mutex.cpp
Last active July 4, 2021 13:19
ticket-based fair mutex implementation
#include <condition_variable>
#include <mutex>
#include <thread>
class fair_mutex {
std::mutex mtx_;
std::condition_variable cv_;
unsigned int next_, curr_;
public:
@yohhoy
yohhoy / prngsize.cpp
Created December 19, 2013 00:37
dump size of PRNG objects in C++11 Standard.
#include <random>
#include <iostream>
int main()
{
std::cout <<
sizeof(std::size_t) << "\n"
"minstd_rand0 " << sizeof(std::minstd_rand0) << "\n"
"minstd_rand " << sizeof(std::minstd_rand) << "\n"
"mt19937 " << sizeof(std::mt19937) << "\n"
@yohhoy
yohhoy / atomicswap.cpp
Last active August 29, 2015 13:56
2 adjacent pointers swap with CMPXCHG8B instruction
#include <atomic>
// pointer pack
typedef struct {
int* p[2];
} pack_t;
void atomic_swap(std::atomic<pack_t>& pack)
{
pack_t actual{{nullptr, nullptr}};
@yohhoy
yohhoy / cpp11mm.md
Last active August 29, 2015 14:04
term of C++11(n3337) memory model
  • byte(1.7/p1): 一意なアドレスを持つ, 少なくとも8ビット幅を持つ基本ストレージ単位.
  • memory location(1.7/p3): スカラ型オブジェクト or 隣接する非0幅ビットフィールドの最大列
  • conflict(1.10/p4): 同一memory locationに対する, 少なくとも一方が変更を行う, 2つのアクションにより生じる.
  • data race(1.10/p21): 少なくとも一方が非atomicで, それぞれ他方に先行(hb)しない, 異なるスレッド上での2つのconflictするアクションで生じる. あらゆるdata raceは未定義の動作(undefined behavior)を引き起こす.

アクション

  • evaluation(1.9/p12): 値の計算(value computation) or side effectの開始 (本文書中では"アクション"と表記)
  • side effect(1.9/p12): 実行環境の状態を変化させるアクション. オブジェクトの更新 or volatile変数へのアクセス or I/Oライブラリ関数の呼出
  • visible side effect(1.10/p13): スカラ型オブジェクトM上の値の計算(value computation)Bに対するM上のvisible side effect Aは, (A hb B) かつ (A hb X かつ X hb Bを満たすM上のside effect Xが存在しない) とき, アクションBはアクションAでM上へ書込まれた値を読出す.
@yohhoy
yohhoy / smptetc.c
Last active November 5, 2015 11:59
NDF/DF SMPTE timecode, frame number w/ FFmpeg
// gcc -std=c99 -I/usr/include/ffmpeg ndf.c -lavutil
#include <stdio.h>
#include <libavutil/timecode.h>
int main() {
AVRational fps = { 30000, 1001 };
AVTimecode tc1, tc2;
av_timecode_init(&tc1, fps, 0, 0, NULL); // NDF TC
av_timecode_init(&tc2, fps, AV_TIMECODE_FLAG_DROPFRAME, 0, NULL); // DF TC