Araq / malebolgia

Malebolgia creates new spawns. Structured concurrency. Thread pools and related APIs.
MIT License
104 stars 10 forks source link

Blocking main thread can cause low thread utilization #26

Closed tsoj closed 4 months ago

tsoj commented 10 months ago

When the main thread takes longer than most worker threads, it leads to low thread utilization, which is unfortunate.

import malebolgia

import std/[os, atomics]

var
    threadpool = createMaster()
    busyThreads: Atomic[int]
busyThreads.store 0

let mainThreadId = getThreadId()

proc findStartPositionsAndPlay() =
    atomicInc busyThreads
    echo "Started. Busy threads: ", busyThreads.load

    sleep(1000)
    if getThreadId() == mainThreadId:
        sleep(10_000)

    atomicDec busyThreads
    echo "Finished. Busy threads: ", busyThreads.load

threadpool.awaitAll:
    for i in 1..10000:
        threadpool.spawn findStartPositionsAndPlay()
Araq commented 4 months ago

Now works well with activeProducer=true:


import malebolgia

import std/[os, atomics]

var
    master = createMaster(activeProducer=true)
    busyThreads: Atomic[int]
busyThreads.store 0

let mainThreadId = getThreadId()

proc findStartPositionsAndPlay() =
    atomicInc busyThreads
    echo "Started. Busy threads: ", busyThreads.load

    sleep(1000)
    if getThreadId() == mainThreadId:
        sleep(10_000)

    atomicDec busyThreads
    echo "Finished. Busy threads: ", busyThreads.load

master.awaitAll:
    for i in 1..100:
        master.spawn findStartPositionsAndPlay()