well, i created a few procedures that SEEM to work great for sharing global strings between threads, but i've only tested it on one system so far (win2k) and i don't have access to any machines that run win98/me
so it would be much appreciated if someone else could give this a go
this example is running 4 additional threads (other than the main thread) and it seems to work great on my work computer.
please let me know if you can run this, and let me know what OS you're running.
Make sure you have the debugger enabled.
basically what you should see, is lines of text in the debugger window, with different characters (5 of them) in the same order over and over. i set the string to reset to blank when the length gets to 50.
Code: Select all
Global varhold.b, runreq.b
Procedure.b request(varnum.b,type.b) ; requests access to a variable
;varnum = number representing the variable you want to lock (only 1 - 8 for now)
;type 1 waits until the variable is available, then gets it
;type 2 returns a 1 if access is granted, else returns a 0 (doesn't wait)
Repeat
If runreq = 1
Repeat
Delay(5)
If runreq = 0 Or runreq > 1
breakout = 1
EndIf
Until breakout = 1
breakout = 0
EndIf
runreq + 1
Delay(Random(5))
If runreq > 1
runreq - 1
Debug "bing!"
Else
breakout = 1
EndIf
Until breakout = 1
If varnum <= 8
Select type
Case 1 ; wait until available.
Repeat
If Left(Right(Bin(varhold),varnum),1) = "1"
Delay(5)
Else
varhold + Pow(varnum,2)
lock = 1
EndIf
Until lock = 1
Case 2 ; immediate (with return code)
If Left(Right(Bin(varhold),varnum),1) = "1"
ProcedureReturn 0
Else
varhold + Pow(varnum,2)
ProcedureReturn 1
EndIf
ProcedureReturn 1
EndSelect
Else
ProcedureReturn 0
EndIf
runreq -1
EndProcedure
Procedure release(varnum.b) ;releases the var for other use
Repeat
If runreq = 1
Repeat
Delay(5)
If runreq = 0 Or runreq > 1
breakout = 1
EndIf
Until breakout = 1
breakout = 0
EndIf
runreq + 1
Delay(Random(5))
If runreq > 1
runreq - 1
Debug "bing!"
Else
breakout = 1
EndIf
Until breakout = 1
If varnum <= 8
varhold - Pow(varnum,2)
EndIf
Delay(20)
runreq - 1
EndProcedure
Global blah$
Procedure testthread(a)
Repeat
a = request(2,1)
blah$ = blah$ + "-"
release(2)
Delay(25)
ForEver
EndProcedure
Procedure testthread1(a)
Repeat
a = request(2,1)
blah$ = blah$ + "#"
release(2)
Delay(25)
ForEver
EndProcedure
Procedure testthread2(a)
Repeat
a = request(2,1)
blah$ = blah$ + "$"
release(2)
Delay(25)
ForEver
EndProcedure
Procedure testthread3(a)
Repeat
a = request(2,1)
blah$ = blah$ + "&"
release(2)
Delay(25)
ForEver
EndProcedure
If OpenWindow(0,0,0,100,100,#PB_Window_SystemMenu,"bleh") = 0
Debug "couldn't make window"
End
EndIf
CreateThread(@testthread(),0)
CreateThread(@testthread1(),0)
CreateThread(@testthread2(),0)
CreateThread(@testthread3(),0)
Repeat
WindowEventv = WindowEvent()
a = request(2,1)
blah$ = blah$ + "+"
Debug blah$
If Len(blah$) > 50
blah$ = ""
EndIf
release(2)
Delay(25)
Until WindowEventv = #PB_Event_CloseWindow

