View previous topic :: View next topic |
Author |
Message |
Skit3000 Admin Team
Joined: 11 May 2002 Posts: 2166 Location: The Netherlands
|
Posted: Sun May 22, 2005 2:28 pm Post subject: Multi threading |
|
|
Recently I found a little article about Multi Threading on the IBasic forum. I converted it to VDS as far as I could, and it is already able to start a new thread. The only thing I can't get to work, is the 4th parameter of the CreateThread function, which should be used to pass variables or text to the newly created thread. Does anybody have any ideas?
Warning: You have to compile this code to an EXE and run the EXE file itself, don't start the program from within the IDE because it will then crash on shutdown.
Code: | # Sources:
# - http://www.pyxia.com/community/viewtopic.php?t=9799
# - http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/createthread.asp
#define command,mt
#define function,mt
option decimalsep,.
# The code below checks if a label should be started insteed of
# the normal code.
if @mt(label)
goto @mt(label)
end
DIALOG CREATE,Test Dialog,-1,0,240,272
REM *** Gewijzigd door de Dialoog Ontwerper op 22-5-2005 - 16:21 ***
DIALOG ADD,EDIT,EDIT1,10,30,180,20
DIALOG ADD,BUTTON,BUTTON1,40,30,180,20,Start a new thread
DIALOG ADD,BUTTON,BUTTON2,190,30,180,20,Give a value to "%a"
DIALOG ADD,BUTTON,BUTTON3,220,30,180,20,Read out the value
DIALOG ADD,TEXT,TEXT1,160,30,,,These buttons are just for testing:
DIALOG SHOW
:Evloop
wait event
%%Event = @event()
if %%Event
goto %%Event
end
goto Evloop
:Button1button
# The line below should create a new thread and it
# should go to the label :Test
mt label, Test
# Run the code below while the new thread is executing
%%Text = Busy doing something...
%%Timer = 55
repeat
dialog set,EDIT1,%%Text
%%Text = @strdel(%%Text,1,1)@substr(%%Text,1,1)
%%Timer = @pred(%%Timer)
wait 0.1
until @equal(%%Timer,0)
goto Evloop
:Button2button
# Give %a a value and show the memory address of the variable
%a = Just some value
info @addr("%a")
goto Evloop
:Button3button
# This code can show the value of a variable if you specify
# the memory address
%c = @input(Enter a value)
info @mt(getvar, %c)
goto Evloop
:Close
# This will stop the current thread
mt stop
exit
:Test
# This code should be called by MT when creating a new thread
info New thread started!
wait 5
info New thread stopped!
stop
:mt
if @equal(%1,label)
if @unequal(%2,)
#mt label, <label>
loadlib kernel32.dll
# %a will contain the Thread ID of the newly created child
%a = @fill(10)
# %b contains the label which should be called in the new child
%b = %2@chr(0)
# This will start the new thread.
%9 = @lib(kernel32.dll,CreateThread,INT:,0,0,@sum($401BE0),@addr("%b"),0,@addr("%a"))
# Check if there occured any errors.
if @unequal(%9,0)
# No errors or whatsoever.
# @val(@substr(%a,1,2)) = Child Thread ID
# @lib(kernel32,GetCurrentThreadId,INT:,) = Own Thread ID
else
# Something went wrong
error 39
end
freelib kernel32.dll
exit
else
# @mt(label)
# This should return the label specified with mt label, <label>
# THIS SHOULD STILL BE MADE!!!
exit
end
end
if @equal(%1,stop)
# mt stop
# This should stop this thread from running
loadlib kernel32.dll
%x = @lib(kernel32.dll,ExitThread,INT:,0)
freelib kernel32.dll
end
if @equal(%1,getvar)
# @mt(getvar, <varid>)
# <varid> = @addr("%varname")
%a = @fill(1024)
loadlib kernel32
# Put the value of %2 into %A
%z = @lib(kernel32,RtlMoveMemory,INT:,@addr("%a"),%2,@len(%a))
%a = @trim(%a)
freelib kernel32
exit %a
end
exit |
_________________ [ Add autocomplete functionality to your VDS IDE windows! ]
Voor Nederlandse beginners met VDS: bekijk ook eens deze tutorial! |
|
Back to top |
|
|
FreezingFire Admin Team
Joined: 23 Jun 2002 Posts: 3508
|
Posted: Mon May 23, 2005 12:01 am Post subject: |
|
|
Interesting... _________________ FreezingFire
VDSWORLD.com
Site Admin Team |
|
Back to top |
|
|
Skit3000 Admin Team
Joined: 11 May 2002 Posts: 2166 Location: The Netherlands
|
Posted: Mon May 23, 2005 3:59 pm Post subject: |
|
|
If I can find a way to pass on a variable on creation of the new thread, I can make something to make subroutines which don't affect the main program when you run them, which can be useful when you are doing creating server programs or programs like a virusscanner...
Another example of something for which multithreading can be useful to, is a program which manipulates files or large strings. The original thread can be used to show the status, while the second thread executes the real code. This way, your application will stay available for user input all the time, while the engine stay on executing... _________________ [ Add autocomplete functionality to your VDS IDE windows! ]
Voor Nederlandse beginners met VDS: bekijk ook eens deze tutorial! |
|
Back to top |
|
|
jwfv Valued Contributor
Joined: 19 Mar 2002 Posts: 422 Location: Beaufort, SC
|
Posted: Mon May 23, 2005 6:27 pm Post subject: |
|
|
I can think of tons of uses for that functionality. _________________ Joe Floyd |
|
Back to top |
|
|
Skit3000 Admin Team
Joined: 11 May 2002 Posts: 2166 Location: The Netherlands
|
|
Back to top |
|
|
Garrett Moderator Team
Joined: 04 Oct 2001 Posts: 2149 Location: A House
|
Posted: Mon May 23, 2005 8:44 pm Post subject: |
|
|
Who can't think of a ton of things to do with multithreading ability!!!
I could use it for a ton of things I have right now.
I'll send you a box of virtual chocolate chip cookies if you get this all
sorted.
(·:.) <- partially eaten virtual chocolate chip cookie
-Garrett |
|
Back to top |
|
|
LiquidCode Moderator Team
Joined: 05 Dec 2000 Posts: 1751 Location: Space and Time
|
Posted: Tue May 24, 2005 1:56 am Post subject: |
|
|
Yes that would be great. I could use that to create plugins for programs. I'll send you virtual Ice cream cones ( )>...well kind of... LOL _________________ Chris
Http://theblindhouse.com |
|
Back to top |
|
|
Skit3000 Admin Team
Joined: 11 May 2002 Posts: 2166 Location: The Netherlands
|
|
Back to top |
|
|
Aslan Valued Contributor
Joined: 31 May 2001 Posts: 589 Location: Memphis, TN USA
|
Posted: Tue May 24, 2005 10:00 pm Post subject: |
|
|
Why do you have %a and %b in quotes when using the @addr() function?
eg.
Code: | %9 = @lib(kernel32.dll,CreateThread,INT:,0,0,@sum($401BE0),@addr("%b"),0,@addr("%a")) |
|
|
Back to top |
|
|
Skit3000 Admin Team
Joined: 11 May 2002 Posts: 2166 Location: The Netherlands
|
Posted: Wed May 25, 2005 6:51 am Post subject: |
|
|
The @addr("%a") will receive the handle of the new thread, so you can control it from the parent thread. The @addr("%b") function is there to pass on a value to the new thread, in this case the parameter of the "mt label" command, so that the new thread knows what to do. My problem is that I am not able to read out the value of that @addr("%b") pointer with the new thread.
This is what MSDN says about the 4th parameter:
MSDN wrote: | lpParameter
[in] Pointer to a variable to be passed to the thread. |
To retreive the lpParameter variable, you should call a function like "GetThreadStartInformation", which unfortunately is a Windows Server 2003 only function... _________________ [ Add autocomplete functionality to your VDS IDE windows! ]
Voor Nederlandse beginners met VDS: bekijk ook eens deze tutorial! |
|
Back to top |
|
|
Skit3000 Admin Team
Joined: 11 May 2002 Posts: 2166 Location: The Netherlands
|
Posted: Wed May 25, 2005 1:12 pm Post subject: |
|
|
Ok, I found another way of doing this by setting a variable which can be accessed from every thread inside the process. But when the new thread is loaded, it seems that a lot of functions which require the kernel32.dll (or well, I guess) are simply skipped, like the "wait 10" command in the ": Testlabel" label below:
Code: | # Sources:
# - http://www.pyxia.com/community/viewtopic.php?t=9799
# - http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/createthread.asp
#define command,mt
#define function,mt
option decimalsep,.
# A dialog should be created before the @mt(label) can be used.
DIALOG CREATE,New Dialog,-1,0,240,160
# The code below checks if a label should be started insteed of
# the normal code.
if @mt(label)
goto @mt(label)
end
DIALOG CREATE,Test Dialog,-1,0,240,272
REM *** Gewijzigd door de Dialoog Ontwerper op 22-5-2005 - 16:21 ***
DIALOG ADD,EDIT,EDIT1,10,30,180,20
DIALOG ADD,BUTTON,BUTTON1,40,30,180,20,Start a new thread
DIALOG SHOW
:Evloop
wait event
%%Event = @event()
if %%Event
goto %%Event
end
goto Evloop
:Button1button
# The line below should create a new thread and it
# should go to the label :Testlabel
mt label, Testlabel
# Run the code below while the new thread is executing
%%Text = Busy doing something...
%%Timer = 55
repeat
dialog set,EDIT1,%%Text
%%Text = @strdel(%%Text,1,1)@substr(%%Text,1,1)
%%Timer = @pred(%%Timer)
wait 0.1
until @equal(%%Timer,0)
goto Evloop
:Close
# This will stop the current thread
mt stop
exit
:Testlabel
# This code should be called by MT when creating a new thread
info New thread started!
# Simulate heavy usage
wait 10
info New thread stopped!
mt stop
:mt
if @equal(%1,label)
if @unequal(%2,)
#mt label, <label>
# Creates a new thread which will load the <label> label.
loadlib kernel32.dll
# -----------------------------------------------------------------------------------
# Set the environment variable
%a = label
%b = %2
%x = @lib(kernel32.dll,SetEnvironmentVariableA,BOOL:,INT:@addr("%a"),INT:@addr("%b"))
# -----------------------------------------------------------------------------------
# %a will contain the Thread ID of the newly created child
%a = @fill(10)
# %b contains the label which should be called in the new child
%b = %2@chr(0)
# This will start the new thread.
%9 = @lib(kernel32.dll,CreateThread,INT:,0,0,@sum($401BE0),@addr("%b"),0,@addr("%a"))
# Check if there occured any errors.
if @unequal(%9,0)
# No errors or whatsoever.
# @val(@substr(%a,1,2)) = Child Thread ID
# @lib(kernel32,GetCurrentThreadId,INT:,) = Own Thread ID
else
# Something went wrong
error 39
end
freelib kernel32.dll
exit
else
# @mt(label)
# This should return the label specified with mt label, <label>
# THIS SHOULD STILL BE MADE!!!
# -----------------------------------------------------------------------------------
loadlib kernel32.dll
%a = label
%b = @fill(100)
%x = @lib(kernel32.dll,GetEnvironmentVariableA,BOOL:,INT:@addr("%a"),INT:@addr("%b"),@len(%b))
freelib kernel32.dll
# -----------------------------------------------------------------------------------
exit @trim(%b)
end
end
if @equal(%1,stop)
# mt stop
# This should stop this thread from running
loadlib kernel32.dll
%x = @lib(kernel32.dll,ExitThread,INT:,0)
freelib kernel32.dll
end
exit |
_________________ [ Add autocomplete functionality to your VDS IDE windows! ]
Voor Nederlandse beginners met VDS: bekijk ook eens deze tutorial! |
|
Back to top |
|
|
Aslan Valued Contributor
Joined: 31 May 2001 Posts: 589 Location: Memphis, TN USA
|
Posted: Wed May 25, 2005 2:44 pm Post subject: |
|
|
Sorry, I'm still a bit confused
Code: | # Set the environment variable
%a = label
%b = %2
%x = @lib(kernel32.dll,SetEnvironmentVariableA,BOOL:,INT:@addr("%a"),INT:@addr("%b")) |
You set %a and %b but by putting them in quotes in the @lib() function you are passing the text "%a" and "%b" instead of "label" and whatever %2 is, respectfully. Or are the quotes needed to get the address of the variable itself.
Ummm, I think I just answered my own question
Nevermind |
|
Back to top |
|
|
jules Professional Member
Joined: 14 Sep 2001 Posts: 1043 Location: Cumbria, UK
|
Posted: Wed May 25, 2005 3:26 pm Post subject: |
|
|
Skit3000 wrote: | Ok, I found another way of doing this by setting a variable which can be accessed from every thread inside the process. But when the new thread is loaded, it seems that a lot of functions which require the kernel32.dll (or well, I guess) are simply skipped, like the "wait 10" command in the ": Testlabel" label below:
|
I didn't try any of this, but it seems like you might be running into a problem I encountered when combining sme DSUs. You can loadlib as many times as you like, but it takes only one freelib to free it.
It's better to loadlib at the start if the program, and free at the end of it, then your DSU cannot affect anthing else that might be using functions from the same DLL. Actually, you don't even need to free it, because VDS will do that when the program terminates. _________________ The Tech Pro
www.tech-pro.net |
|
Back to top |
|
|
briguy Contributor
Joined: 09 Aug 2007 Posts: 79
|
Posted: Tue Apr 28, 2009 2:08 pm Post subject: |
|
|
Any update to this code? Is this a dead project or semi working? |
|
Back to top |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You can attach files in this forum You can download files in this forum
|
|