Hi Pritpal,
> Please tell me where I am mistaking.
>
> thread static aFld := { { '', 0 } }
> static mtx
> proc main()
> local aThd := array( 5 )
> local i
> CLEAR SCREEN
> if ! hb_mtvm()
> ? "No MT support, quitting!"
> return nil
> endif
> mtx := hb_mutexCreate()
> for i := 1 to 5
> aThd[ i ] := hb_threadStart( @thFunc() )
> next
> aeval( aThd, {|hThd| hb_threadJoin( hThd ) } )
> return
>
> func thFunc()
> local n
> local aFields := aFld
> hb_mutexLock( mtx )
> n := ascan( aFields, {|e| e[ 1 ] == 'ALIAS' } )
> if n > 0
> ? 'Oh, it must never been there as aFields is a thread static! '+str(
> ThreadId(),2 )
> else
> aadd( aFields, { 'ALIAS',212 } )
> endif
> hb_mutexUnlock( mtx )
> return nil
> //-------------------------------------------------------//
>
> I assume that "thread static" is different for each thread but above example
> suggests otherwise. It is breaking my first attempts to port my applications
> to MT mode.
>
> It took me a while to locate the problem. OR may be I need to learn
> more about file wide thread statics .
It's not related to thread statics which works as expected. You are confused
by array behavior in Clipper and [x]Harbour. Each thread has it's own thread
static aFld variable but in each of them aFld points to the same array.
Exactly the same thing you can make using any type of variable also in
single thread program, f.e.:
proc main()
local a1, a2
a1 := { "" }
a2 := a1
? len( a1 ), len( a2 )
aadd( a1, "2" )
? len( a1 ), len( a2 )
return
As you can see when array in a1 is changed then the same happen with array
in a though both are two different local variables but both keeps reference
to exactly the same array body. In your above example you replicated such
situation but using thread static variables.
If you are using complex variables like array or hashes and you want to make
their separated copies then you have to initialize aFld inside the thread or
clone aFld body when thFunc starts.
best regards,
Przemek
_______________________________________________
Harbour mailing list
[email protected]
http://lists.harbour-project.org/mailman/listinfo/harbour