PBXB64 Threading

Threading & XTHREAD Standard

Full multi-threading support with THREAD CREATE/STATUS/CLOSE/SUSPEND/RESUME/SET PRIORITY/ID and XTHREAD CREATE for extended thread control. Real Win32 thread API (CreateThread, SuspendThread, ResumeThread, SetThreadPriority, GetExitCodeThread, CloseHandle).

THREAD CREATE - Spawn Threads

Thread creation and control
#COMPILE EXE #DIM ALL DECLARE FUNCTION WorkerThread() AS LONG FUNCTION PBMAIN() AS LONG LOCAL hThread AS QUAD LOCAL status AS LONG ' Create thread (the entry point must be a FUNCTION) THREAD CREATE WorkerThread() TO hThread ' Poll for completion: &H103 (259) means "still running" THREAD STATUS hThread TO status DO WHILE status = &H103 SLEEP 100 THREAD STATUS hThread TO status LOOP PRINT "Thread exited with: "; status ' 7 THREAD CLOSE hThread FUNCTION = 0 END FUNCTION FUNCTION WorkerThread() AS LONG PRINT "Working in background..." SLEEP 500 FUNCTION = 7 ' becomes the thread exit code END FUNCTION

THREAD CREATE spawns a new thread whose entry point is a FUNCTION (a SUB is not allowed). THREAD STATUS polls without blocking: &H103 means the thread is still running, anything else is its exit code. THREAD CLOSE releases the handle. THREAD SUSPEND/RESUME/SET PRIORITY provide full control.

XTHREAD CREATE - Extended Threads

Extended thread creation
FUNCTION PBMAIN() AS LONG LOCAL hThread AS LONG LOCAL param AS LONG param = 42 ' XTHREAD with parameter XTHREAD CREATE WorkerWithParam(param) TO hThread SLEEP 1000 THREAD CLOSE hThread FUNCTION = 0 END FUNCTION FUNCTION WorkerWithParam(BYVAL p AS LONG) AS LONG PRINT "Parameter: "; p FUNCTION = p * 2 END FUNCTION

XTHREAD CREATE supports passing a parameter to the thread function. The thread function returns a value that can be retrieved via THREAD STATUS. Ideal for compute tasks with input/output.

Back to feature list