The Computer Language
Benchmarks Game

thread-ring Python 3 #2 program

source code

# The Computer Language Benchmarks Game
# http://benchmarksgame.alioth.debian.org/
# Contributed by Antti Kervinen
# Modified by Tupteq
# 2to3

import sys
import _thread

# Set minimum stack size for threads, otherwise the program may fail
# to create such a many threads
_thread.stack_size(32*1024)

def threadfun(number, lock_acquire, next_release):
    global n
    while 1:
        lock_acquire()
        if n > 0:
            n -= 1
            next_release()
        else:
            print(number)
            main_lock.release()

# main
n = int(sys.argv[1])
main_lock = _thread.allocate_lock()
main_lock.acquire()

first_lock = _thread.allocate_lock()
next_lock = first_lock

for number in range(503):
    lock = next_lock
    lock.acquire()
    next_lock = _thread.allocate_lock() if number < 502 else first_lock
    _thread.start_new_thread(threadfun,
        (number+1, lock.acquire, next_lock.release))

first_lock.release()
main_lock.acquire()
    

notes, command-line, and program output

NOTES:
32-bit Ubuntu one core
Python 3.5.0 (default, Sep 14 2015, 09:36:50) 
[GCC 4.9.2] on linux


Tue, 15 Sep 2015 21:00:23 GMT

MAKE:
mv threadring.python3-2.python3 threadring.python3-2.py
0.01s to complete and log all make actions

COMMAND LINE:
/usr/local/src/Python-3.5.0/bin/python3.5 threadring.python3-2.py 50000000

PROGRAM OUTPUT:
292