PBXB64 Threading
Threading & XTHREAD Standard
Vollständige Multi-Threading-Unterstützung with THREAD CREATE/STATUS/CLOSE/SUSPEND/RESUME/SET PRIORITY/ID and XTHREAD CREATE for erweiterte Thread-Steuerung. Echte Win32-Thread-API (CreateThread, SuspendThread, ResumeThread, SetThreadPriority, GetExitCodeThread, CloseHandle).
THREAD CREATE - Spawn Threads
Thread-Erzeugung und -Steuerung
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 SUBTHREAD 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
Erweiterte Thread-Erzeugung
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 FUNCTIONXTHREAD CREATE supports Übergabe eines Parameters an die Thread-Funktion. The thread function returns a value that can be retrieved via THREAD STATUS. Ideal für Rechenaufgaben with input/output.