AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CriticalSection() - Real Multithreading for AutoHotkey.dll

 
Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
HotKeyIt



Joined: 18 Jun 2008
Posts: 4608
Location: AHK Forum

PostPosted: Thu Nov 19, 2009 2:44 pm    Post subject: CriticalSection() - Real Multithreading for AutoHotkey.dll Reply with quote

Using CriticalSection, you can write and read variables in several threads at the same time.

How to use wrote:
- lpCriticalSection:=CriticalSection() ;create and Initialize a new CriticalSection and return pointer which can be used in Lock() and UnLock().

- Lock(lpCriticalSection) ;EnterCriticalSection - other threads will be waiting for your code to finish

- UnLock(lpCriticalSection) ;DeleteCriticalSection - now other threads can access the variable(s).

- CriticalSection(lpCriticalSection) ;deletes CriticalSection

- CriticalSection() ;deletes all CriticalSections


Example should explain everything else.

You will need AutoHotkey.dll and AhkDllThread as well as CreateScript

Here you will find more information: MSDN.

Example requires AutoHotkey_H from this package
Code:
SetBatchLines,-1 ;Max performance
Loop 2 ;Create 2 CriticalSections and return pointer to use with Lock() and UnLock()
   _C%A_Index%:=CriticalSection()
;Get pointer to Variables to use in Alias()
pX:=getVar(x),pY:=getVar(y)

ThreadScript=
(
  #Persistent
  Alias(x,%pX% + 0),Alias(y,%pY% + 0) ;create Alias variables x+y, so you can use same variables in exe and dll
  Loop 2
     SetTimer,Label`%A_Index`%,50 ;Label1 and Label2 will be launched rapidly
  Return
   Label1:
    Lock(%_C1%)
    x.="new"
    UnLock(%_C1%)
  Return

  Label2:
    Lock(%_C2%)
    y.="newtext"
    UnLock(%_C2%)
)
dll:=AhkDllThread("AutoHotkey.dll")
dll.ahktextdll(ThreadScript)
Loop
  {
    Loop 2
      Lock(_C%A_Index%)
   lengthX:=StrLen(x),lengthY:=StrLen(y)
    Loop 2
      UnLock(_C%A_Index%)
    ToolTip % lengthX "-" lengthY
  }
Esc::ExitApp

To run it with AutoHotkey_L you will need LowLevel:
Code:
SetBatchLines,-1 ;Max performance
LowLevel_Init()
;Create CriticalSection and return pointer to use with Lock() and UnLock()
Loop 2
   _C%A_Index%:=CriticalSection()
;Get pointer to Variables to use in Alias()
pX:=__getVar(x),pY:=__getVar(y)

ThreadScript=
(
  #Persistent
  Alias(x,%pX% + 0),Alias(y,%pY% + 0) ;create Alias variables x+y, so you can use same variables in exe and dll
  Loop 2
     SetTimer,Label`%A_Index`%,50 ;Label1 and Label2 will be launched rapidly
  Return
   Label1:
    Lock(%_C1%)
    x.="new"
    UnLock(%_C1%)
  Return

  Label2:
    Lock(%_C2%)
    y.="newtext"
    UnLock(%_C2%)
)
dll:=AhkDllThread("AutoHotkey.dll")
dll.ahktextdll(ThreadScript)
Loop
  {
    Loop 2
      Lock(_C%A_Index%)
   lengthX:=StrLen(x),lengthY:=StrLen(y)
    Loop 2
      UnLock(_C%A_Index%)
    ToolTip % lengthX "-" lengthY
  }
Esc::ExitApp

CriticalSection.ahk Note Exclamation AutoHotkey_H and AHK.dll have a build in function Lock() + UnLock(), they are faster than user defined functions
Therefore if you are using AHK_H, delete the Lock and UnLock functions from CriticalSection.ahk.
Code:
Lock(LPCRITICAL_SECTION){
  DllCall("EnterCriticalSection","UInt",LPCRITICAL_SECTION)
}
UnLock(LPCRITICAL_SECTION){
  DllCall("LeaveCriticalSection","UInt",LPCRITICAL_SECTION)
}

CriticalSection(LPCRITICAL_SECTION="ÿÿÿÿ"){
  static
  If (LPCRITICAL_SECTION!="" and LPCRITICAL_SECTION!="ÿÿÿÿ"){
    DllCall("DeleteCriticalSection","Uint",LPCRITICAL_SECTION)
    Return
  } else if (LPCRITICAL_SECTION=""){
    Loop % (count){
      DllCall("DeleteCriticalSection","Uint",&CriticalSection%count%)
      count:=0
    }
    Return
  }
  count++
  VarSetCapacity(CriticalSection%count%,24)
  DllCall("InitializeCriticalSection","Uint",&CriticalSection%count%)
  Return &CriticalSection%count%
}

_________________
AHK_H (2alpha) AHF TT _Struct WatchDir Yaml _Input ObjTree RapidHotkey DynaRun Wink


Last edited by HotKeyIt on Wed Jan 18, 2012 11:57 am; edited 6 times in total
Back to top
View user's profile Send private message
Futurity



Joined: 13 Feb 2007
Posts: 21

PostPosted: Thu Jan 28, 2010 11:55 am    Post subject: Reply with quote

Great job!

I will use it in my script.

One problem though, it does not work with AutoHotkey_N41:
http://www.autohotkey.net/~HotKeyIt/AutoHotkey_SOURCE_N41.zip

When I put a msgbox after
Thread(ThreadScript "`nInclude:IncludeEnd"),
the script never reaches it.

It works fine, with:
http://www.autohotkey.net/~tinku99/ahkdll/release/AutoHotkey_N10.zip
Back to top
View user's profile Send private message
HotKeyIt



Joined: 18 Jun 2008
Posts: 4608
Location: AHK Forum

PostPosted: Thu Jan 06, 2011 2:41 pm    Post subject: Reply with quote

I have changed examples to work with most current AHK.dll.
_________________
AHK_H (2alpha) AHF TT _Struct WatchDir Yaml _Input ObjTree RapidHotkey DynaRun Wink
Back to top
View user's profile Send private message
n-l-i-d
Guest





PostPosted: Thu Jan 06, 2011 4:24 pm    Post subject: Reply with quote

Where are LockSec() and UnLockSec()?

I used AutoHotkey_H, copied the necessary code and included the files mentioned...

Quote:

Error: Call to nonexistent function.

Specifically: LockSec(_C%A_Index%)


Question
Back to top
HotKeyIt



Joined: 18 Jun 2008
Posts: 4608
Location: AHK Forum

PostPosted: Thu Jan 06, 2011 7:08 pm    Post subject: Reply with quote

Sorry it should be Lock() and UnLock(), I have updated examples.
_________________
AHK_H (2alpha) AHF TT _Struct WatchDir Yaml _Input ObjTree RapidHotkey DynaRun Wink
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group