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
#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 startet einen neuen Thread, dessen Einstiegspunkt eine FUNCTION ist (eine SUB ist nicht erlaubt). THREAD STATUS fragt ohne zu blockieren ab: &H103 bedeutet, dass der Thread noch läuft, jeder andere Wert ist sein Exit-Code. THREAD CLOSE gibt das Handle frei. THREAD SUSPEND/RESUME/SET PRIORITY bieten die volle Kontrolle.

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 FUNCTION

XTHREAD 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.

Zurück zur Feature-Liste