Skunksoft ThreadSync Lib

Everything else that doesn't fall into one of the other PB categories.
PolyVector
Enthusiast
Enthusiast
Posts: 499
Joined: Wed Sep 17, 2003 9:17 pm
Location: Southern California
Contact:

Skunksoft ThreadSync Lib

Post by PolyVector »

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...

Edit: The Library's been updated a few times.... Check out the new Message Commands!
I should have put this in announcements :roll:

Tell me whatcha think! :)
http://www.skunksoft.com/Downloads/SS_ThreadSync.zip

If you need help, visit our forums:
http://www.skunksoft.com/forums/

The site's a complete mess, we're undergoing massive reorganization[/b]
Last edited by PolyVector on Tue Apr 27, 2004 7:40 pm, edited 3 times in total.
User avatar
NoahPhense
Addict
Addict
Posts: 1999
Joined: Thu Oct 16, 2003 8:30 pm
Location: North Florida

Re: Skunksoft ThreadSync Lib

Post by NoahPhense »

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
http://www.skunksoft.com/Downloads/SS_ThreadSync.zip
If you need help, visit our forums:
http://www.skunksoft.com/forums/

Sorry if the site's a complete mess, we're undergoing massive reorganization
Would you post an example of how you are utilizing this lib?

- np
PolyVector
Enthusiast
Enthusiast
Posts: 499
Joined: Wed Sep 17, 2003 9:17 pm
Location: Southern California
Contact:

Post by PolyVector »

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

Code: Select all

;-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.
dige
Addict
Addict
Posts: 1416
Joined: Wed Apr 30, 2003 8:15 am
Location: Germany
Contact:

Post by dige »

Sounds very interesting! Does that solve the thread unsafety problem?

cya dige
PolyVector
Enthusiast
Enthusiast
Posts: 499
Joined: Wed Sep 17, 2003 9:17 pm
Location: Southern California
Contact:

Post by PolyVector »

Yup :)
I'll try to come up with some better examples/docs...
I'm usually very busy tho'
User avatar
NoahPhense
Addict
Addict
Posts: 1999
Joined: Thu Oct 16, 2003 8:30 pm
Location: North Florida

..

Post by NoahPhense »

PolyVector wrote:Yup :)
I'll try to come up with some better examples/docs...
I'm usually very busy tho'
Very kewl, thanks for the research. I will have more questions later.. ;)

- np
PolyVector
Enthusiast
Enthusiast
Posts: 499
Joined: Wed Sep 17, 2003 9:17 pm
Location: Southern California
Contact:

Post by PolyVector »

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 :D
It looks MUCH cleaner then all that Mutex_() stuff....
I hope this lib works out for everyone else as well.
-->PolyVector
Post Reply