PolyVector wrote:Hello everyone,
I created a tiny lib for my own use that syncronizes threads...
I figured some of you may be interested... It is VERY simple to use and only has 5 commands
Tell me whatcha think!
1) hResource=AddThreadResource() ;Creates an ID to reference a shared 'Resource'... Call this at the beginning of your program
2) OpenThreadResource(hResource) ;Waits until the 'Resource' is available, then makes it unavailable to other threads...
3) OpenThreadResourceWithin(hResource,ResourceTimeout) ;Same as above, only w/ a timeout...
4) CloseThreadResource(hResource) ;Makes a 'Resource' available to other threads...
5) DeleteThreadResource(hResource) ;Free's the 'Resource' handle... Call this at the end of you program
To be honest I haven't really used it yet... I added an example for the Lib and DLL versions and updated the zip... You should be able to make as many 'Resources' as you want with no problems... Edit: Updated Code
;-Skunksoft ThreadSync Lib Test
;-Created by PolyVector...
;/This example shows the use of every command in the library so far...
;/MyAddThread() does the useless task of adding 1 to a global variable
;/MySubtractThread() does the useless task of subtracting 1 from a global variable
;/Run with the Debugger On to watch the threads fight eachother :)
;ThreadSyncInit() ;Initializes the SS_ThreadSync Engine
;ThreadSyncExit() ;Exits the SS_ThreadSync Engine
;hResource=AddThreadResource() ;Creates an ID to reference a shared 'Resource'... Call this at the beginning of your program
;OpenThreadResource(hResource) ;Waits until the 'Resource' is available, then makes it unavailable to other threads...
;OpenThreadResourceWithin(hResource,ResourceTimeout) ;Same as above, only with a timeout...
;CloseThreadResource(hResource) ;Makes a 'Resource' available to other threads...
;DeleteThreadResource(hResource) ;Free's the 'Resource' handle... Call this at the end of you program
;SendThreadMessage(RecipientID,MessageID) ;Sends a Message to the RecipientID
;GetThreadMessage(RecipientID) ;Retrieves the MessageID sent to the RecipientID
;-
ThreadSyncInit();The Lib version now requires you to call Init and Exit procedures
Enumeration ;Recipients for ThreadMessages :)
#MyAddThread
#MySubtractThread
EndEnumeration
Enumeration 1 ;MessageID's to send (can't use 0 for messages)
#LetsQuit
EndEnumeration
;Make some global variables for testing...
Global LongVariable.l
Global StringVariable$
;Create a 'Resource' to protect the variables...
Global MyResource.l
MyResource=AddThreadResource()
Procedure MyAddThread(RecipientID)
Repeat
Select GetThreadMessage(RecipientID);Check for any messages send to this thread
Case #LetsQuit
Debug "Bye Bye From MyAddThread()"
ProcedureReturn
EndSelect
OpenThreadResource(MyResource) ;Wait for MyResource to become available
LongVariable=LongVariable+1
StringVariable$=Str(LongVariable)
Debug "+1 = "+StringVariable$
CloseThreadResource(MyResource) ;Free up MyResource for other threads...
ForEver
EndProcedure
Procedure MySubtractThread(RecipientID)
Repeat
Select GetThreadMessage(RecipientID);Check for any messages send to this thread
Case #LetsQuit
Debug "Bye Bye From MySubtractThread()"
ProcedureReturn
EndSelect
If OpenThreadResourceWithin(MyResource,500) ;Wait up to 500ms for MyResource to be available
LongVariable=LongVariable-1
StringVariable$=Str(LongVariable)
Debug "-1 = "+StringVariable$
CloseThreadResource(MyResource) ;Free up MyResource for other threads...
Else
;"We Timed Out... Let's do something else for now :)"
EndIf
ForEver
EndProcedure
If OpenWindow(0,0,0,80,60,#PB_Window_SystemMenu,"ThreadSync Test")
hMyAddThread=CreateThread(@MyAddThread(),#MyAddThread);Tell the new thread it's RecipientID :)
hMySubtractThread=CreateThread(@MySubtractThread(),#MySubtractThread);Tell the new thread it's RecipientID :)
Repeat:Until WaitWindowEvent()=#wm_close
CloseWindow(0)
SendThreadMessage(#MyAddThread,#LetsQuit);Tell MyAddThread() we want to quit
SendThreadMessage(#MySubtractThread,#LetsQuit);Tell MySubtractThread() we want to quit
WaitThread(hMyAddThread);Wait for thread to exit
WaitThread(hMySubtractThread);Wait for thread to exit
Debug "Quit"
DeleteThreadResource(MyResource);Delete the MyResource handle
ThreadSyncExit();The Lib version now requires you to call Init and Exit procedures
EndIf
Last edited by PolyVector on Wed Apr 28, 2004 6:15 am, edited 4 times in total.
My library is now in use in all of my Skunksoft projects, and I'm pleased to say there have been no errors so far
It looks MUCH cleaner then all that Mutex_() stuff....
I hope this lib works out for everyone else as well.
-->PolyVector