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

Discuss the development of AutoHotkey_H
arcticir
Posts: 693
Joined: 17 Nov 2013, 11:32

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

12 Apr 2016, 11:35

Thanks.

I slightly modified: :)

Code: Select all

exethread(s:="",p:=""){
	return lib:=GetModuleHandle(),{(""):ThreadID:=NewThread(s,p)
	  ,_ahkFunction:DynaCall(GetProcAddress(lib,"ahkFunction"),"s==sttttttttttui","",0,0,0,0,0,0,0,0,0,0,ThreadID)
	  ,_ahkPostFunction:DynaCall(GetProcAddress(lib,"ahkPostFunction"),"i==sttttttttttui","",0,0,0,0,0,0,0,0,0,0,ThreadID)
	  ,ahkFunction:DynaCall(GetProcAddress(lib,"ahkFunction"),"s==sssssssssssui","",0,0,0,0,0,0,0,0,0,0,ThreadID)
	  ,ahkPostFunction:DynaCall(GetProcAddress(lib,"ahkPostFunction"),"i==sssssssssssui","",0,0,0,0,0,0,0,0,0,0,ThreadID)
	  ,addFile:DynaCall(GetProcAddress(lib,"addFile"),"ut==siui","",0,ThreadID)
	  ,addScript:DynaCall(GetProcAddress(lib,"addScript"),"ut==siui","",0,ThreadID)
	  ,ahkExec:DynaCall(GetProcAddress(lib,"ahkExec"),"ut==sui","",ThreadID)
	  ,ahkExecuteLine:DynaCall(GetProcAddress(lib,"ahkExecuteLine"),"ut==utuiuiui",0,0,0,ThreadID)
	  ,ahkFindFunc:DynaCall(GetProcAddress(lib,"ahkFindFunc"),"ut==sui","",ThreadID)
	  ,ahkFindLabel:DynaCall(GetProcAddress(lib,"ahkFindLabel"),"ut==sui","",ThreadID)
	  ,ahkgetvar:DynaCall(GetProcAddress(lib,"ahkgetvar"),"s==suiui","",0,ThreadID)
	  ,ahkassign:DynaCall(GetProcAddress(lib,"ahkassign"),"s==ssui","","",ThreadID)
	  ,ahkLabel:DynaCall(GetProcAddress(lib,"ahkLabel"),"ui==suiui","",0,ThreadID)
	  ,ahkPause:DynaCall(GetProcAddress(lib,"ahkPause"),"ui==sui","",ThreadID)
	  ,ahkIsUnicode:DynaCall(GetProcAddress(lib,"ahkIsUnicode"),"ui==")}
}

Code: Select all

	this.hThreadID:=NewThread("global A_ParentThread`nA_ParentThread(){`nstatic _:=A_ParentThread()`nA_ParentThread:=ObjShare(" ObjShare(new _ThreadClass) ")`nObjShare(" ObjShare(this) ").RegisterThread(ObjShare(ThreadObj_Class()))`n}`n" script,cmdLine)
look forward to solve memory problems .

-------------

Also, I have an idea I have said to you: to increase the definition of the title.
I found that for multi-threaded, it has a huge role.
It allows us to easily identify the thread.
For example, it is clear to know which thread pops up "MsgBox"

Code: Select all

NewThread(Script, cmdLine:="", title:="")
a_ahktitle
Although we can use GlobalStruct () modify it,
But it uses too much time.

-------------
Also, About thread parameters
In my experience, many threads need to pass parameters. Otherwise, it takes more time to take the initiative to get the data.
We can support it?
Its difficulty is to pass object parameters

Code: Select all

NewThread(Script, cmdLine:="", title:="", param:="")
a_ahkparam
For example the menu , it uses five threads. Parameter will reduce the interaction data.
Image
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)

12 Apr 2016, 13:07

I'm not sure if you know about this already, but if the main Thread of AutoHotkey terminates before any of the children then we get a rather weird behavior.
But that brings up another Problem.
How are you going to handle Script Termination?
Recommends AHK Studio
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, 14:10

I have improved ExeThread even more and implemented parameter for Title/A_ScriptName in newthread.

@nnnik: Main thread tries to exit all threads before exiting, if threads don't exit within around 1 second main threads terminates.
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, 17:51

arcticir wrote:Also, About thread parameters
In my experience, many threads need to pass parameters. Otherwise, it takes more time to take the initiative to get the data.
We can support it?
Simply pass an object pointer or include the pointer in script.

Code: Select all

obj:=CriticalObject({test:"Hello World!"})
NewThread("obj:=CriticalObject(A_Args.1)`nMsgBox `% obj.test",(&obj) "","Test")
MsgBox end

Code: Select all

obj:=CriticalObject({test:"Hello World!"})
NewThread("obj:=CriticalObject(" (&obj) ")`nMsgBox `% obj.test",,"Test")
MsgBox end
arcticir
Posts: 693
Joined: 17 Nov 2013, 11:32

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

13 Apr 2016, 00:30

I was so resolved, but I think some trouble. :D

I feel the need to make "A_ParentThread" having a "global and static" attribute .

Code: Select all

this.hThreadID:=NewThread("global A_ParentThread`nA_ParentThread(){`nstatic 
In order to avoid this situation :

Code: Select all

test(){
static x:=A_ParentThread.xxxx
}

@nnnik My method is more complicated. Using threads Console and PUM menu. But not difficult.
HotKeyIt
Posts: 2364
Joined: 29 Sep 2013, 18:35
Contact:

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

13 Apr 2016, 05:23

I have modified ExeThread() function to allow passing a threadID instead creating a Thread :)
Therefore the first parameter must be empty string and second parameter the threadID.

Code: Select all

var:="test"
NewThread("A_ParentThread:=ExeThread(`"`"," GetCurrentThreadId() ")`nMsgBox `% A_ParentThread.ahkgetvar.var")
MsgBox End
arcticir
Posts: 693
Joined: 17 Nov 2013, 11:32

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

13 Apr 2016, 06:34

thanks.
Must be resolved , memory cleanup problem , otherwise all talk . :(
arcticir
Posts: 693
Joined: 17 Nov 2013, 11:32

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

14 Apr 2016, 12:17

If this problem can not be resolved , then we only use AutoHotkey.dll approach. Reuse threads to save memory.
HotKeyIt
Posts: 2364
Joined: 29 Sep 2013, 18:35
Contact:

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

16 Apr 2016, 07:37

Fixed some bugs and added ahkReady support + ahkReady method to ExeThread().
HotKeyIt
Posts: 2364
Joined: 29 Sep 2013, 18:35
Contact:

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

17 Apr 2016, 17:51

Update wrote:Improved exiting threads and added ahkterminate() method to exit thread created by ExeThread()
Barbossa155
Posts: 30
Joined: 13 Feb 2016, 07:31

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

25 Apr 2016, 09:17

oh nice
i replaced only the Autohotkey.exe from w64 folder

i tried your first example @ startpost and it says me something like

Image



what is to do?
thx
HotKeyIt
Posts: 2364
Joined: 29 Sep 2013, 18:35
Contact:

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

25 Apr 2016, 14:52

There was a bug and example was not updated to use correct syntax, download new version and try new example from first post ;)
HotKeyIt
Posts: 2364
Joined: 29 Sep 2013, 18:35
Contact:

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

25 Apr 2016, 23:55

I have changed back to use MSVCR100.dll for all modules and removed AutoHotkeySC.bin.
HotKeyIt
Posts: 2364
Joined: 29 Sep 2013, 18:35
Contact:

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

29 Apr 2016, 13:55

I have implemented multi-threaded Objects and removed CriticalObject as there is no need for it anymore, the objects will be now always invoked in the thread which created them.
HotKeyIt
Posts: 2364
Joined: 29 Sep 2013, 18:35
Contact:

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

07 May 2016, 05:53

I have reverted and fixed CriticalObject() and fixed Title for NewThread as well as a fix for SimpleHeap.
HotKeyIt
Posts: 2364
Joined: 29 Sep 2013, 18:35
Contact:

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

08 May 2016, 02:37

I have improved exported functions and fixed some bugs when exiting script. Also fixed AhkExported() to work for main script.

Return to “Development”

Who is online

Users browsing this forum: No registered users and 10 guests