TJF wrote: fxm wrote:
AGS wrote:But I don't get the without Dim Shared: I thought Dim Shared was needed to make sure your code is thread safe ;)
I do not understand your remark above.
Scroll down the code block :)
Now let's be serious!
I think that 'Dim Shared'
is a method for beginners to pass variables to a thread.
, it is best to use the 'Byval As Any Ptr'
argument as a pointer to a data structure (UDT, array, ...).
See my previous other post (including 3 proposed syntaxes in examples):viewtopic.php?p=167651#p167651
In the example of the manual (KeyPgThreadCall), one of the data is a common mutex handle ('Any Ptr'
) in order to synchronize threads between themselves.
In the version using 'Threadcreate'
+ UDT structure, as presently no static member data can be defined, an emulation of a static member data may be used as following:
Code: Select all
' Threading using "ThreadCreate" + UDT untyped pointer
Dim id As String
Dim count As Integer
Declare Static Function tlock (Byval param As Any Ptr = 0) As Any Ptr
Static Function thread_data.tlock (Byval param As Any Ptr = 0) As Any Ptr
Static p As Any Ptr
p = Iif(param > 0, param, p)
Function = p
Sub thread( Byval param As Any Ptr )
With *Cast(thread_data Ptr, param)
For i As Integer = 1 To .count
Print "thread "; .id, i; "/"; .count
Dim pA As thread_data Ptr = New thread_data
pA->id = "A"
pA->count = 6
Dim a As Any Ptr = ThreadCreate(@thread, pA)
Dim pB As thread_data Ptr = New thread_data
pB->id = "B"
pB->count = 4
Dim b As Any Ptr = ThreadCreate(@thread, pB)
Print "All done (and also without Dim Shared!)"