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
DECLARE SUB WorkerThread() FUNCTION PBMAIN() AS LONG LOCAL hThread AS LONG LOCAL threadID AS LONG LOCAL status AS LONG ' Create thread THREAD CREATE WorkerThread() TO hThread, threadID ' Wait for completion THREAD STATUS hThread TO status WHILE status = 0 SLEEP 100 THREAD STATUS hThread TO status WEND PRINT "Thread exited with: "; status THREAD CLOSE hThread FUNCTION = 0 END FUNCTION SUB WorkerThread() PRINT "Working in background..." SLEEP 500 END SUB

THREAD CREATE spawns a new thread with a PB subroutine as entry point. THREAD STATUS polls the 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