All you will need to use new multi-threading feature is AutoHotkey.exe from Win32w or x64w folder.
I have run some tests and it works quite good so far but I need your help as more testing is required. Any feedback is welcome
New features wrote:
Multi-threading is only available in AutoHotkey.exe (AutoHotkeySC.bin will be discontinued as we can compile AutoHotkey.exe)
New thread can be started via build-in function ThreadID := NewThread(Script, cmdLine:="", Title:=""), included functionExeThread(script, cmdLine:="", Title:="") or included functionThreadObj := ThreadObj(Script, cmdLine:="", Title:=""). To load a script from file simply use thead:=ThreadObj("#include MyFile.ahk"). While NewThread() returns only the ThreadID, ExeThread() and ThreadObj() allows to interact with new thread
ExeThread has the same methods as AhkExported() plus ahkReady.
ThreadObj allows following methods.
Also in new thread you can access the parent thread that created the new thread via A_ParentThread object using same methods.
The Hook exists only once, so all Hotkeys and Hotstrings as well as Input are shared to all threads, this way we get errors when a hotkey or hotstring exists in another thread already and even Input can be cancelled (ErrorLevel="NewInput") from another thread.
Debugger was also improved to support multi-threading.
IsLabel := ThreadObj.Gosub(LabelName,-1)
IsLabel := ThreadObj.Gosub(LabelName,1000)
IsLabel := ThreadObj.Gosub(LabelName)
Gosub(s,r:=""){
if IsLabel(s)
{
if (r="")
Gosub % s
else
SetTimer % s, % r
Return 1
}
}
Re: [AHK_H v2] Native multi-threading using thread local storage (without AutoHotkey.dll)
Posted: 10 Apr 2016, 06:59
by HotKeyIt
Did you replace all dll Threads by ThreadObj, if not I think the problem is very likely that previous v2 is compiled using Multithreaded-DLL (/MD)?
Since it is v2 you can easily run any command as function, I don't think we need a GoSub to do that.
I have changed CallFunc to Call and changed VarContent to __Get and Assign to __Set
EDIT:
I have removed all methods that can be called via .Call.
Re: [AHK_H v2] Native multi-threading using thread local storage (without AutoHotkey.dll)
Posted: 10 Apr 2016, 08:38
by HinkerLoden
Maybe that fix my problems and push the performance with the Gdip lib. But i have to much projects atm to finish. I put the 2 libs into the attachment. Maybe you can build a benchmark with that.
Re: [AHK_H v2] Native multi-threading using thread local storage (without AutoHotkey.dll)
Posted: 10 Apr 2016, 08:57
by arcticir
I did not modify my code, I only replaced AHK.EXE.
It seems that we need more examples, such as
Compare the difference between old and new.
As well as the new features.
I GOOGLE the TLS
But unfortunately I have a lot of places do not understand.
So I need more examples.
I notice that you use a lot of NewThread() AND ObjShare()
I did not find NewThread () code.
It seems that it triggered a huge change
But no matter what , this is good progress. thank you.
--------------
About ObjShell ()
I found some errors.
I use it to load SQLite3()
10 threads to read-write using database
It is frequently wrong.
So I no longer use it.
Continue to use, CriticalObject ().
Re: [AHK_H v2] Native multi-threading using thread local storage (without AutoHotkey.dll)
Posted: 10 Apr 2016, 09:24
by arcticir
Create a new thread to use too much time.
ThreadObj 100 ms
AhkThread 60 ms
ahkLoad 30 ms
#Singleinstance FORCE
r:=Counter()
f:=ahkLoad()
f.ahktextdll("MsgBox `% Counter(" r ")")
MsgBox
Counter(s:=0) {
static Frequency := QueryFrequency()
DllCall("QueryPerformanceCounter",Int64P,Counter)
Return (s ? (Counter-s)/Frequency : Counter)
}
QueryFrequency(){
DllCall("QueryPerformanceFrequency",Int64P,F)
Return F
}
ahkLoad(s:=""){
f:={"":r:=MemoryLoadLibrary(A_ahkDir "\AutoHotkey" (s?(s=1?"Mini":s=2?"2":"Mini2"):"") ".dll"), Type:s}
for i,n in {"ahkFunction":"s==sssssssssss","ahkPostFunction":"i==sssssssssss","ahkdll":"ut==sss","ahktextdll":"ut==sss","ahkReady":"","ahkReload":"i==i"
,"ahkTerminate":"i==i","addFile":"ut==sucuc","addScript":"ut==si","ahkExec":"ui==s","ahkassign":"ui==ss","ahkExecuteLine":"ut==utuiui"
,"ahkFindFunc":"ut==s","ahkFindLabel":"ut==s","ahkgetvar":"s==sui","ahkLabel":"ui==sui","ahkPause":"i==s","ahkIsUnicode":""}
f[i]:=DynaCall(MemoryGetProcAddress(r,i),n)
return f
}
Re: [AHK_H v2] Native multi-threading using thread local storage (without AutoHotkey.dll)
Posted: 10 Apr 2016, 09:32
by nnnik
Why would you need to create new Threads super fast?
Re: [AHK_H v2] Native multi-threading using thread local storage (without AutoHotkey.dll)
Posted: 10 Apr 2016, 11:17
by arcticir
This is not a good question.
Do not you want to delay it as small as possible?
100 ms, has a very noticeable delay.
If "gosub", have such a delay, you do not care?
My pursuit, load a new thread, like loading a new label as fast.
Because I use a script console, it can be extremely frequent calls the new thread.
Re: [AHK_H v2] Native multi-threading using thread local storage (without AutoHotkey.dll)
Posted: 10 Apr 2016, 12:12
by HotKeyIt
I did not modify my code, I only replaced AHK.EXE.
It seems that we need more examples, such as
Compare the difference between old and new.
As well as the new features.
You would most likely need to modify your code as you cannot share memory between exe and dll because ahkexe uses msvcrt.dll and ahkdll msvcr100.dll. Only AutoHotkey[Mini].dll can share memory to another AutoHotkey.dll instance and all threads created with NewThreads can share memory to each other and main thread.
So you will to change all ahktextdll calls to NewThread or ThreadObj.
Any example for AutoHotkey.dll can be used with NewThread or ThreadObj but you need to change the methods like thread.ahkFunction to thread.Call, thread.ahkgetvar.var to thread.var, ...
The differences for new version are mentioned in first post, the only new feature is built-in function NewThread().
ThreadObj won't and can't be faster due to ObjShare(), NewThread() is a bit faster than ahktextdll
#Singleinstance FORCE
r:=Counter()
NewThread("MsgBox NewThread:`t`%Counter(" r ")`%`n" CreateScript("Counter{}`nQueryFrequency{}"))
f:=ahkLoad()
Sleep 100
r:=Counter()
f.ahktextdll("MsgBox AhkLoad:`t`%Counter(" r ")`%`n" CreateScript("Counter{}`nQueryFrequency{}"))
MsgBox
Counter(s:=0) {
static Frequency := QueryFrequency()
DllCall("QueryPerformanceCounter",Int64P,Counter)
Return (s ? (Counter-s)/Frequency : Counter)
}
QueryFrequency(){
DllCall("QueryPerformanceFrequency",Int64P,F)
Return F
}
ahkLoad(s:=""){
f:={"":r:=MemoryLoadLibrary(A_ahkDir "\AutoHotkey" (s?(s=1?"Mini":s=2?"2":"Mini2"):"") ".dll"), Type:s}
for i,n in {"ahkFunction":"s==sssssssssss","ahkPostFunction":"i==sssssssssss","ahkdll":"ut==sss","ahktextdll":"ut==sss","ahkReady":"","ahkReload":"i==i"
,"ahkTerminate":"i==i","addFile":"ut==sucuc","addScript":"ut==si","ahkExec":"ui==s","ahkassign":"ui==ss","ahkExecuteLine":"ut==utuiui"
,"ahkFindFunc":"ut==s","ahkFindLabel":"ut==s","ahkgetvar":"s==sui","ahkLabel":"ui==sui","ahkPause":"i==s","ahkIsUnicode":""}
f[i]:=DynaCall(MemoryGetProcAddress(r,i),n)
return f
}
Re: [AHK_H v2] Native multi-threading using thread local storage (without AutoHotkey.dll)
Posted: 10 Apr 2016, 12:17
by HotKeyIt
HinkerLoden wrote:Maybe that fix my problems and push the performance with the Gdip lib. But i have to much projects atm to finish. I put the 2 libs into the attachment. Maybe you can build a benchmark with that.
It won't be faster than using AutoHotkey.dll, benchmark is not required.
Re: [AHK_H v2] Native multi-threading using thread local storage (without AutoHotkey.dll)
Posted: 10 Apr 2016, 13:45
by arcticir
Unable to shorten the time, it is a pity
But I think I can use NewThread + CriticalObject to raise some speed.
But I'm pleasantly surprised to find memory usage significantly reduced.
Also, thread can not re-used.
This brings a new problem: how to completely clear the memory used by a thread.
Although a thread, memory usage is significantly reduced, but not without.
This example , memory usage, rising from 3M to 15M.
Re: [AHK_H v2] Native multi-threading using thread local storage (without AutoHotkey.dll)
Posted: 10 Apr 2016, 16:16
by HotKeyIt
This is mostly due to ObjShare function used to communicate between threads. It should not be used with regards to MSDN docs but I have not found anything else that could do the job like that.
So to clear the memory, all you need is to exit the thread.
When I find time I will have a look if we can clear 100% of resources used by NewThread.
Re: [AHK_H v2] Native multi-threading using thread local storage (without AutoHotkey.dll)
Posted: 10 Apr 2016, 22:42
by arcticir
I think this is the problem that must be overcome .
If we prolonged use ThreadObj, accumulated a large amount of memory.
Only use "psapi.dll \ EmptyWorkingSet", This bad choice .
NewThread not seem to be used alone, this is wrong:
Re: [AHK_H v2] Native multi-threading using thread local storage (without AutoHotkey.dll)
Posted: 11 Apr 2016, 05:21
by HotKeyIt
NewThread only returns the ThreadID, probably I could change the exported functions to accept the ThreadID so they can be used the same way as AutoHotkey.dll.
Re: [AHK_H v2] Native multi-threading using thread local storage (without AutoHotkey.dll)
Posted: 11 Apr 2016, 17:22
by TLM
ouuu just noticed this.. testing....
tytyty
Re: [AHK_H v2] Native multi-threading using thread local storage (without AutoHotkey.dll)
Posted: 12 Apr 2016, 08:59
by HotKeyIt
I have implement multi-threading support for exported functions, now a new function is available threadObj := ExeThread(script, cmdLine := "")