ich ziele darauf ab einen Reads-Write Lock zu programmieren (multiple Leser, ein Schreiber). Ich bin mir noch nicht sicher ob ich ausschließlich PB Objekte nutzen möchte, oder etwas selbst auf dem Low Level schreiben will - bei Letzterem komme ich schon etwas ins Trudeln. Ein Konzept für so einen RW Lock, der auch multiple Selbst-Locks zulässt (also wenn der aktuelle Schreiber nochmal schreiben oder lesen will z.B.), benötigt da die ThreadID damit diese in einer Liste aufgenommen oder beim Unlock() rausgenommen wird.
Lange Rede kurzer Sinn: beim Versuch mir die ThreadID's über die API zu holen erhalte ich verschiedene Werte bei ThreadId() und by GetCurrentThreadID() unter Windows. Unter Linux funktioniert das aber mit pthread_self_() als GetCurrentThreadID_() pendant, mit der Eigenart dass der Main-Thread dem B Thread im dem Beispiel identisch sind (vermutlich normales fork() verhalten, weiss nicht).
Bug oder feature? Das dürfte keine Probleme bei meinem Vorhaben verursachen, ich dachte aber es sollte mal angesprochen werden. Mache ich bei Windows was falsch?
Code: Alles auswählen
; ==============================================================
; === (c) 2022 by benubi ===
; ==============================================================
;
; Source: GetCurrentThreadID.pb
; Thread: -/-
; Author: benubi
; Date: 28/10/2022
; OS: All
; License: 100% free
;
; ==============================================================
; Description, Beschreibung
;
; EN Access the current thread & process ID's
;
; FR Accès aux identifiants des tâches & procéssus courants
;
; DE Zugriff auf laufende Thread & Prozess ID's
;
;
; ==============================================================
;
Procedure.i GetCurrentThreadID() ; Returns the OS' ThreadID of the calling thread. This is a different value from ThreadID().
CompilerSelect #PB_Compiler_OS
CompilerCase #PB_OS_Windows
ProcedureReturn GetCurrentThreadId_() ; WINDOWS
CompilerCase #PB_OS_Linux
ProcedureReturn pthread_self_() ; Linux / Posix
CompilerCase #PB_OS_MacOS
ProcedureReturn pthread_self_() ; MacOS / Posix
CompilerCase #PB_OS_AmigaOS
ProcedureReturn pthread_self_() ; AmigaOS / Posix
CompilerEndSelect
EndProcedure
Procedure.i GetProcessID() ; Returns the current ProcessID of the running executable
CompilerSelect #PB_Compiler_OS
CompilerCase #PB_OS_Windows
ProcedureReturn GetCurrentProcessId_() ; Windows
CompilerCase #PB_OS_Linux
ProcedureReturn getpid_() ; Linux / Posix
CompilerCase #PB_OS_MacOS
ProcedureReturn getpid_() ; MacOS / Posix
CompilerCase #PB_OS_AmigaOS
ProcedureReturn getpid_() ; AmigaOS / Posix
CompilerEndSelect
EndProcedure
CompilerIf #PB_Compiler_IsMainFile
; ==============================================================
; ==============================================================
; Demo
; ==============================================================
; ==============================================================
CompilerIf Not #PB_Compiler_Thread
DebuggerWarning("WARNING: ThreadSafe is OFF. It should be turned ON or the Debug-strings could mess up.")
CompilerEndIf
Procedure A_Proc(abc)
Debug LSet("",100,"-")
Debug #PB_Compiler_Procedure + "-ProcessID: " + GetProcessID()
Debug #PB_Compiler_Procedure + "-ThreadID : " + GetCurrentThreadID()
EndProcedure
Procedure B_Proc(abc)
Debug LSet("",100,"-")
Debug #PB_Compiler_Procedure + "-ProcessID: " + GetProcessID()
Debug #PB_Compiler_Procedure + "-ThreadID : " + GetCurrentThreadID()
EndProcedure
Define th1, th2
Debug "Main process ID: " + GetProcessID()
Debug "Main thread ID : " + GetCurrentThreadID() ; <- main thread / process thread
th1 = CreateThread(@A_Proc(), 0)
th2 = CreateThread(@B_Proc(), 0)
Debug "A_proc Th1:"+th1+" Th1-ID : "+ThreadID(th1)
Debug "B_Proc Th2:"+th2+" Th2-ID : "+ThreadID(th2)
If IsThread(th1)
WaitThread(th1)
EndIf
If IsThread(th2)
WaitThread(th2)
EndIf
Debug LSet("",100,"-")
Debug "Good bye."
End
CompilerEndIf