kuroko/test/testThreading.krk
K. Lange 6f95124bcd Threading
Don't be deceived by its size: this commit is mostly a bunch of
sed replacements to move thread state out of "vm" and introduce
a thread-local "KrkThreadState" object to hold stack pointers and
call frames for individual threads.

Threading support is incomplete and there's almost definitely a
bunch of really easy ways to break everything just by allocating
objects, but really simple stuff like carefully modifying a
a list of ints should work okay.

The README has been rewritten to remove code samples, which have
moved to the Wiki (as has the section on integrating with C).

Some other stuff has also moved around because the threading changes
necessitated a major API break anyway, so I also took the time to
fix some ABI issues.
2021-02-09 18:51:09 +09:00

49 lines
1.2 KiB
Python

import os
if 'KUROKO_TEST_ENV' in os.environ:
return 0
let Thread
try:
from threading import Thread as _Thread
Thread = _Thread
except:
print("Threading is not available.")
return 0
class Summer(Thread):
def __init__(self, lst, min, max):
self.lst = lst
self.min = min
self.max = max
self.result = None
def run(self):
self.result = 0
for i = self.min; i < self.max; i++:
self.result += self.lst[i]
if __name__ == '__main__':
import kuroko
let numThreads = 10
if len(kuroko.argv) > 1:
numThreads = int(kuroko.argv[1])
print("Starting {} threads.".format(numThreads))
let threads = []
let totalCount = 5000000
let chunkSize = totalCount / numThreads
let numbers = list(range(totalCount))
let cnt = 0
if totalCount > chunkSize * numThreads:
chunkSize += 1
for i in range(numThreads):
let newThread = Summer(numbers, cnt, (cnt + chunkSize) if (cnt + chunkSize < totalCount) else totalCount)
threads.append(newThread)
newThread.start()
cnt += chunkSize
let total = 0
for thread in threads:
thread.join()
total += thread.result
print("Got",total)