' Thread Pool — Multiple threads
#COMPILE EXE
#DIM ALL

FUNCTION WorkerProc(BYVAL n AS LONG) AS LONG
    PRINT "Thread " + STR$(n) + " started"
    SLEEP 500
    PRINT "Thread " + STR$(n) + " finished"
    FUNCTION = n
END FUNCTION

FUNCTION PBMAIN() AS LONG
    LOCAL h1  AS LONG
    LOCAL h2  AS LONG
    LOCAL h3  AS LONG
    THREAD CREATE WorkerProc(1) TO h1
    THREAD CREATE WorkerProc(2) TO h2
    THREAD CREATE WorkerProc(3) TO h3
    THREAD CLOSE h1
    THREAD CLOSE h2
    THREAD CLOSE h3
    PRINT "All threads launched"
    FUNCTION = 0
END FUNCTION
