Skip to content

Instantly share code, notes, and snippets.

@chenshuo
chenshuo / waiter.h
Last active August 25, 2025 14:20
A handful of implementations of Waiter class for discussion.
#include <boost/noncopyable.hpp>
#include <pthread.h>
#include <stdlib.h>
// a superfluous check for pedantic people
inline void CHECK_SUCCESS(int ret)
{
if (ret != 0)
{
abort();
@chenshuo
chenshuo / linux_com.h
Created March 12, 2011 10:14
If Linux kernel were exposed as COM interfaces
class Linux_0_01
{
int restart_syscall(); //0
int exit(); //1
int fork(); //2
int read(); //3
int write(); //4
int open(); //5
int close(); //6
int waitpid(); //7
@chenshuo
chenshuo / multiarray.cc
Last active October 16, 2023 14:29
MultiArray
#include <assert.h>
#include <array>
#include <functional>
#include <numeric>
template<typename T, int N>
class MultiArrayBase
{
public:
MultiArrayBase(T* data, const std::array<int, N>& dim)
@chenshuo
chenshuo / query_freq.cc
Created November 3, 2012 22:59
Sort queries by their frequencies.
// answer to http://weibo.com/1915548291/z2UtyzuvQ
// see also http://www.cnblogs.com/baiyanhuang/archive/2012/11/11/2764914.html
#include <boost/bind.hpp>
#include <boost/function.hpp>
#include <boost/noncopyable.hpp>
#include <boost/ptr_container/ptr_vector.hpp>
#include <fstream>
#include <iostream>
@chenshuo
chenshuo / multiple_thread.sh
Created September 4, 2010 07:13
ping pong test for muduo network library
#!/bin/sh
killall server
timeout=${timeout:-100}
bufsize=16384
for nosessions in 100 1000; do
for nothreads in 1 2 3 4; do
sleep 5
echo "Bufsize: $bufsize Threads: $nothreads Sessions: $nosessions"
@chenshuo
chenshuo / nginx.conf
Created March 3, 2012 17:01
nginx conf using http echo module for '/hello'
#user nobody;
worker_processes 4;
events {
worker_connections 10240;
}
http {
include mime.types;
default_type application/octet-stream;
@chenshuo
chenshuo / tree-bench.cc
Last active October 9, 2021 07:48
STL tree structure and benchmark.
#include <set>
#include <stdio.h>
#include <sys/time.h>
double now()
{
struct timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_sec + tv.tv_usec / 1000000.0;
@chenshuo
chenshuo / score_rank.cc
Created March 3, 2012 16:13
Update score and get rank in real time
#include <vector>
#include <assert.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
typedef int32_t Score;
typedef int32_t UserId;
typedef int32_t Rank;
const Score kInvalidScore = -1;
@chenshuo
chenshuo / nqueens.cc
Created December 11, 2013 23:03
Multithreaded N-queens in C++11
#include <algorithm>
#include <atomic>
#include <thread>
#include <vector>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <sys/time.h>
@chenshuo
chenshuo / Einstein.java
Created November 16, 2012 06:14
Einstein's Puzzle
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import JaCoP.constraints.Alldifferent;
import JaCoP.constraints.Distance;
import JaCoP.constraints.XeqC;
import JaCoP.constraints.XeqY;
import JaCoP.constraints.XplusCeqZ;
import JaCoP.core.Domain;