[AHK_H v2] Native multi-threading using thread local storage (without AutoHotkey.dll)

Discuss the development of AutoHotkey_H
HotKeyIt
Posts: 2364
Joined: 29 Sep 2013, 18:35
Contact:

[AHK_H v2] Native multi-threading using thread local storage (without AutoHotkey.dll)

07 Apr 2016, 10:27

AutoHotkey_H v2 tls has been now merged to AutoHotkey_H v2, see NewThread, ExeThread and ThreadObj.
Let me know if you find any problems ;)
AutoHotkey_H v2 tls changes
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: [AHK_H v2] Native multi-threading using thread local storage (without AutoHotkey.dll)

08 Apr 2016, 05:06

I've been waiting for this.
Recommends AHK Studio
ItCrashes

Re: [AHK_H v2] Native multi-threading using thread local storage (without AutoHotkey.dll)

08 Apr 2016, 05:36

When I run your test script on Windows 7, 32 bit, it crashes after I try to close the last MsgBox (first one that pops up).
[Window Title]
AutoHotkey_H Unicode 32-bit

[Main Instruction]
AutoHotkey_H Unicode 32-bit has stopped working

[Content]
Windows is checking for a solution to the problem...
ItDoesntCrash

Re: [AHK_H v2] Native multi-threading using thread local storage (without AutoHotkey.dll)

08 Apr 2016, 10:10

Same test script no longer crashes, exits nicely as it should. :thumbup:
arcticir
Posts: 693
Joined: 17 Nov 2013, 11:32

Re: [AHK_H v2] Native multi-threading using thread local storage (without AutoHotkey.dll)

09 Apr 2016, 23:10

After I replaced the old version , can not run , it seems that there are compatibility issues .

I think it is more reasonable to use :

Code: Select all

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
	}
}
HotKeyIt
Posts: 2364
Joined: 29 Sep 2013, 18:35
Contact:

Re: [AHK_H v2] Native multi-threading using thread local storage (without AutoHotkey.dll)

10 Apr 2016, 06:59

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 :)

Code: Select all

thread:=ThreadObj("#Persistent`nReturn`nLabel:`nMsgBox `% A_Now`nReturn")
thread.Call("SetTimer","Label",-1)
thread.var:="Hello World!"
MsgBox % thread.var
MsgBox end
EDIT:
I have removed all methods that can be called via .Call.
User avatar
HinkerLoden
Posts: 93
Joined: 23 Mar 2016, 07:50
Contact:

Re: [AHK_H v2] Native multi-threading using thread local storage (without AutoHotkey.dll)

10 Apr 2016, 08:38

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.
Attachments
Gdip_FastImageSearch.ahk
FastImageSearch
(19.94 KiB) Downloaded 496 times
Gdip_All.ahk
All Platform GDIP LIB
(95.38 KiB) Downloaded 480 times
arcticir
Posts: 693
Joined: 17 Nov 2013, 11:32

Re: [AHK_H v2] Native multi-threading using thread local storage (without AutoHotkey.dll)

10 Apr 2016, 08:57

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 :D

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 ().
Last edited by arcticir on 10 Apr 2016, 11:31, edited 1 time in total.
arcticir
Posts: 693
Joined: 17 Nov 2013, 11:32

Re: [AHK_H v2] Native multi-threading using thread local storage (without AutoHotkey.dll)

10 Apr 2016, 09:24

Create a new thread to use too much time.
ThreadObj 100 ms
AhkThread 60 ms
ahkLoad 30 ms

I look forward to 15MS Or less

Code: Select all

#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
}

User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: [AHK_H v2] Native multi-threading using thread local storage (without AutoHotkey.dll)

10 Apr 2016, 09:32

Why would you need to create new Threads super fast?
Recommends AHK Studio
arcticir
Posts: 693
Joined: 17 Nov 2013, 11:32

Re: [AHK_H v2] Native multi-threading using thread local storage (without AutoHotkey.dll)

10 Apr 2016, 11:17

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.
HotKeyIt
Posts: 2364
Joined: 29 Sep 2013, 18:35
Contact:

Re: [AHK_H v2] Native multi-threading using thread local storage (without AutoHotkey.dll)

10 Apr 2016, 12:12

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 :)

Code: Select all

#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
}
HotKeyIt
Posts: 2364
Joined: 29 Sep 2013, 18:35
Contact:

Re: [AHK_H v2] Native multi-threading using thread local storage (without AutoHotkey.dll)

10 Apr 2016, 12:17

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.
arcticir
Posts: 693
Joined: 17 Nov 2013, 11:32

Re: [AHK_H v2] Native multi-threading using thread local storage (without AutoHotkey.dll)

10 Apr 2016, 13:45

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.

Code: Select all

loop 400 
{
ThreadObj("#NoTrayIcon`nSleep, 1")
ToolTip,% A_Index
}
MsgBox
HotKeyIt
Posts: 2364
Joined: 29 Sep 2013, 18:35
Contact:

Re: [AHK_H v2] Native multi-threading using thread local storage (without AutoHotkey.dll)

10 Apr 2016, 16:16

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.
arcticir
Posts: 693
Joined: 17 Nov 2013, 11:32

Re: [AHK_H v2] Native multi-threading using thread local storage (without AutoHotkey.dll)

10 Apr 2016, 22:42

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:

Code: Select all

f:=NewThread("#NoTrayIcon`n#Persistent")
f.addScript("Msgbox",1) 
MsgBox,test
HotKeyIt
Posts: 2364
Joined: 29 Sep 2013, 18:35
Contact:

Re: [AHK_H v2] Native multi-threading using thread local storage (without AutoHotkey.dll)

11 Apr 2016, 05:21

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.
HotKeyIt
Posts: 2364
Joined: 29 Sep 2013, 18:35
Contact:

Re: [AHK_H v2] Native multi-threading using thread local storage (without AutoHotkey.dll)

12 Apr 2016, 08:59

I have implement multi-threading support for exported functions, now a new function is available threadObj := ExeThread(script, cmdLine := "") ;)

Return to “Development”

Who is online

Users browsing this forum: No registered users and 18 guests