Skip to content

Instantly share code, notes, and snippets.

@msg555
msg555 / less_unfair_scheduling.cpp
Created Feb 21, 2022
Analog of https://gist.github.com/msg555/dd491078cf10dbabbe7b1cd142644910 in C++. Still not that fair but doesn't permanently starve threads.
View less_unfair_scheduling.cpp
#include <unistd.h>
#include <sys/time.h>
#include <iostream>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <chrono>
using namespace std;
@msg555
msg555 / unfair_queue.py
Created Feb 21, 2022
Example of unfair scheduling using queue.Queue in CPython
View unfair_queue.py
from threading import Thread, get_ident
import time
from queue import Queue
THREAD_COUNT = 10
POOL_GET_TIMEOUT = 10.0
POOL_SIZE = 2
pool = Queue()
for i in range(POOL_SIZE):
@msg555
msg555 / unfiar_scheduling.py
Created Feb 21, 2022
Example of unfair scheduling in CPython's condition variable implementation
View unfiar_scheduling.py
from threading import Condition, Thread
import time
# This is a sample implementation of something like an object pool. It uses
# a condition variable to ensure that at most `counter` threads are allowed
# to execute at a time.
THREAD_COUNT = 10
counter = 1
cnd = Condition()
@msg555
msg555 / replay.php
Created Feb 13, 2021
Replay read/write routines for dustkid
View replay.php
<?php
function _substr($data, $pos, $len) {
if (0 <= $pos && $pos + $len <= strlen($data) && $pos <= $pos + $len) {
return substr($data, $pos, $len);
}
return null;
}
function _unpack_byte($data, $pos) {
$str = _substr($data, $pos, 1);
@msg555
msg555 / subframe_callback.as
Created Jan 21, 2020
Sample script using the subframe callback API.
View subframe_callback.as
class script : callback_base {
scene@ g;
array<dustman@> players;
script() {
@g = get_scene();
}
void step(int num_entities) {
int nc = num_cameras();
View 16.cpp
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int mexp(int v, int e, int MOD) {
int ret = 1;
if (e == 0) return ret;
for(int i = 31 - __builtin_clz(e); i >= 0; i--) {
@msg555
msg555 / 10-integral.cpp
Created Dec 11, 2019
Float free solution to Advent Day 10
View 10-integral.cpp
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
using namespace std;
int gcd(int a, int b) {
return b ? gcd(b, a % b) : a;
@msg555
msg555 / intcode.py
Last active Dec 8, 2019
Solution to day 7
View intcode.py
import abc
import asyncio
import itertools
import sys
class Instruction(metaclass=abc.ABCMeta):
PARAMS = 0
def __init__(self, comp, offset, mode):
View dusttrackerpro.cpp
class script {
scene@ g;
textfield@ txt;
int init_filth;
int init_filth_block;
int init_enemy;
script() {
@g = get_scene();
@msg555
msg555 / sounddemo.cpp
Created Oct 20, 2017
An example of how to embed and use custom sounds with dustscripting.
View sounddemo.cpp
const string EMBED_sound1 = "test1.ogg";
class script {
scene@ g;
script() {
@g = get_scene();
}
void build_sounds(message@ msg) {