Quick little demo of threads fighting over a dict (and still working)

This commit is contained in:
K. Lange 2021-02-10 19:06:25 +09:00
parent 09ce6c46a9
commit 4233234406
2 changed files with 46 additions and 0 deletions

46
test/testNoise.krk Normal file
View File

@ -0,0 +1,46 @@
import os
if 'KUROKO_TEST_ENV' in os.environ:
return 0
from time import sleep
from fileio import open, stdin
from threading import Thread
let d = {}
let stop = False
for y in range(0x40):
for x in range(0x40):
d[(x,y)] = 0
class NoisePainter(Thread):
def run(self):
let myRando = open('/dev/urandom','rb')
while not stop:
let bytes = myRando.read(3)
let x = bytes[0] & 0x3F
let y = bytes[1] & 0x3F
d[(x,y)] = bytes[2]
let painters = [NoisePainter() for i in range(5)]
for painter in painters:
painter.start()
def drawScreen():
print("\[[H",end="")
for y in range(0x20):
for x in range(0x40):
let top = d[(x,y*2)]
let bottom = d[(x,y*2+1)]
print("\[[38;2;{};{};{};48;2;{};{};{}m▀".format(top,top,top,bottom,bottom,bottom),end="")
print("\[[0m")
for i in range(5):
drawScreen()
stop = True
for painter in painters:
painter.join()
drawScreen()

View File