Skip to content

Instantly share code, notes, and snippets.

@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 / SpliteratorDump.java
Created September 11, 2014 00:11
Java8 StreamAPI Spliterator characteristics
import java.util.*;
import java.util.stream.*;
public class SpliteratorDump {
static String c2s(String hdr, int c) {
StringBuilder sb = new StringBuilder(hdr + ":");
if ((c & Spliterator.CONCURRENT) != 0) sb.append(" CONCURRENT");
if ((c & Spliterator.DISTINCT) != 0) sb.append(" DISTINCT");
if ((c & Spliterator.IMMUTABLE) != 0) sb.append(" IMMUTABLE");
if ((c & Spliterator.NONNULL) != 0) sb.append(" NONNULL");
@yohhoy
yohhoy / mt_queue0.cpp
Last active August 29, 2015 14:06
MT-safe queue implementation
// mutex + busy-loop
#include <queue>
#include <thread>
#include <mutex>
class mt_queue {
static const int capacity = 10;
std::queue<int> q_;
std::mutex mtx_;
public:
@yohhoy
yohhoy / mt_queue_deadlock1.cpp
Last active August 29, 2015 14:06
MT-safe queue/Dead-lock senario
//
// Dead-lock senario under wrong implementation (capacity == 1)
//
// Precondition:
// 1-Procuder thread(P1) and 2-Consumer threads(C1,C2)
// Thread status = Running->Blocked->Waiting->Running
//
// Step:
// (queue q_ is empty)
// C1: call pop() and blocked at cv_.wait() [C1: Running->Blocked]
@yohhoy
yohhoy / gist:191757eaa0fe8dfa39e8
Last active August 29, 2015 14:06
Dead-lock demonstration with condition_variable::notify_one()/pthread_cond_signal()
//
// Dead-lock demonstration with condition_variable::notify_one()
//
#include <utility>
#include <mutex>
#include <condition_variable>
template<typename T>
class mt_slot {
T slot_;
@yohhoy
yohhoy / sort_by.cpp
Created October 5, 2014 08:28
sort by data member in class
#include <algorithm>
#include <iterator>
#include <tuple>
template <
typename Iterator, // RandomAccessIterator
typename... Ts,
typename U = typename std::iterator_traits<Iterator>::value_type>
void sort_by(Iterator begin, Iterator end, Ts U::*... pm)
{
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import java.util.concurrent.CancellationException;
public class CompletableFutureExample {
private static CompletableFuture<Optional<String>> applyTaskAsync(String s) {
return CompletableFuture.completedFuture(s)
import java.util.stream.IntStream;
public class StreamCollectFailure {
public static void main(String[] args) {
int[] points = { 80, 95, 70, 60 };
double ave = IntStream.of(points).average().orElse(0.0);
double var = IntStream.of(points).collect(
() -> new Double(0.0),
(r,n) -> { r += Math.pow(n - ave, 2); },
@yohhoy
yohhoy / gist:a3ae53709e39e8b4a34c
Last active August 29, 2015 14:09
Declare function in C
#include <stdio.h>
// F is function type with 2 int args and return int.
typedef int F(int, int);
// declare function `add`, `sub`
F add, sub;
int main()
{