View previous topic :: View next topic |
Author |
Message |
zxuiji Advanced Cheater
Reputation: 1
Joined: 26 Sep 2016 Posts: 70
|
Posted: Mon Oct 22, 2018 7:11 am Post subject: Managing a thread via memory records |
|
|
Have a function that converts a patch code into a list, one of the types is a delay code and in order to implement that correctly I tried using a thread. Have yet to execute my script but would like to check if any potential problems can be seen in it's creation & management while I check other parts of my script:
Code: |
local exec = nil
mr.OnActivate = function(mr,b,c)
exec = true
return true
end
mr.OnDeactivate = function(mr,b,c)
exec = nil
return true
end
local t = createThread(function(t)
local next = 1, n = 1
t.Name = text .. ' Thread'
t.freeOnTerminate(false)
t.OnTerminate
if exec then
for i,v in pairs(codes) do
if n >= next then
next = v()
n = 0
else
next = 0
end
if next < 1 then
break
end
n = n + 1
end
if next == 0 then
syncronize(mr.delete)
end
end)
mr.OnDestroy = function(mr)
t.freeOnTerminate(true)
exec = nil
end
|
_________________
Those who do not trust in christ to save them from their sins will only experience hell when their soul is released from it's earthly vessel. |
|
Back to top |
|
 |
Dark Byte Site Admin
Reputation: 470
Joined: 09 May 2003 Posts: 25783 Location: The netherlands
|
Posted: Mon Oct 22, 2018 8:00 am Post subject: |
|
|
as long as the code in v() doesn't access the gui without synchronize then it should work properly
you may want to destroy the thread when you're done with it
_________________
Do not ask me about online cheats. I don't know any and wont help finding them.
Like my help? Join me on Patreon so i can keep helping |
|
Back to top |
|
 |
zxuiji Advanced Cheater
Reputation: 1
Joined: 26 Sep 2016 Posts: 70
|
Posted: Mon Oct 22, 2018 8:29 am Post subject: |
|
|
Dark Byte wrote: | as long as the code in v() doesn't access the gui without synchronize then it should work properly
you may want to destroy the thread when you're done with it |
v() is just generated functions from raw patch codes, aside from a delay function which just calls sleep() it won't access the GUI so all is good there :)
as for the destroying do you mean I should call t.destroy() or something?
_________________
Those who do not trust in christ to save them from their sins will only experience hell when their soul is released from it's earthly vessel. |
|
Back to top |
|
 |
Dark Byte Site Admin
Reputation: 470
Joined: 09 May 2003 Posts: 25783 Location: The netherlands
|
Posted: Mon Oct 22, 2018 8:32 am Post subject: |
|
|
yes, since free on terminate is false it won't clean up the memory for the thread object
_________________
Do not ask me about online cheats. I don't know any and wont help finding them.
Like my help? Join me on Patreon so i can keep helping |
|
Back to top |
|
 |
zxuiji Advanced Cheater
Reputation: 1
Joined: 26 Sep 2016 Posts: 70
|
Posted: Mon Oct 22, 2018 8:51 am Post subject: |
|
|
Ah I take it you didn't notice the call in Ondestroy()
_________________
Those who do not trust in christ to save them from their sins will only experience hell when their soul is released from it's earthly vessel. |
|
Back to top |
|
 |
Dark Byte Site Admin
Reputation: 470
Joined: 09 May 2003 Posts: 25783 Location: The netherlands
|
Posted: Mon Oct 22, 2018 10:33 am Post subject: |
|
|
That is too late as the thread is likely already finished so it won't check if freeonterminate is true
_________________
Do not ask me about online cheats. I don't know any and wont help finding them.
Like my help? Join me on Patreon so i can keep helping |
|
Back to top |
|
 |
zxuiji Advanced Cheater
Reputation: 1
Joined: 26 Sep 2016 Posts: 70
|
Posted: Mon Oct 22, 2018 6:05 pm Post subject: |
|
|
Hm? Does it not simply go back and repeat the thread then? If not could you give me a modified example please? (You can delete the OnTerminate from it as I removed it later realizing I didn't need it)
_________________
Those who do not trust in christ to save them from their sins will only experience hell when their soul is released from it's earthly vessel. |
|
Back to top |
|
 |
Dark Byte Site Admin
Reputation: 470
Joined: 09 May 2003 Posts: 25783 Location: The netherlands
|
Posted: Tue Oct 23, 2018 12:13 am Post subject: |
|
|
t.destroy() when you're done with it
_________________
Do not ask me about online cheats. I don't know any and wont help finding them.
Like my help? Join me on Patreon so i can keep helping |
|
Back to top |
|
 |
zxuiji Advanced Cheater
Reputation: 1
Joined: 26 Sep 2016 Posts: 70
|
Posted: Tue Oct 23, 2018 2:28 am Post subject: |
|
|
I thought the point of freeOnTerminate(false) was to tell the thread to continuelly repeat itself until either destroy() is called or freeOnTerminate(true) is called, if it's not then how do I get it to do so?
_________________
Those who do not trust in christ to save them from their sins will only experience hell when their soul is released from it's earthly vessel. |
|
Back to top |
|
 |
Dark Byte Site Admin
Reputation: 470
Joined: 09 May 2003 Posts: 25783 Location: The netherlands
|
Posted: Tue Oct 23, 2018 2:55 am Post subject: |
|
|
No, a thread runs it's code and then ends when it reaches the end.
if freeOnTerminate is true (default) it will clean up it's own memory once it has reached the end (but that can be annoying if you want to manually destroy it)
If you want a thread to loop you need to add code for a loop
e.g:
Code: |
while something>10 do
--....
end
|
or
Code: |
while t.Terminated==false do
end
|
(the thread property Terminated gets set to true when .destroy() or .terminate() is called)
_________________
Do not ask me about online cheats. I don't know any and wont help finding them.
Like my help? Join me on Patreon so i can keep helping |
|
Back to top |
|
 |
zxuiji Advanced Cheater
Reputation: 1
Joined: 26 Sep 2016 Posts: 70
|
Posted: Tue Oct 23, 2018 3:08 am Post subject: |
|
|
Thanks, I'll use that last one you mentioned, which out of destroy() and terminate() would allow the thread to finish before destruction?
_________________
Those who do not trust in christ to save them from their sins will only experience hell when their soul is released from it's earthly vessel. |
|
Back to top |
|
 |
Dark Byte Site Admin
Reputation: 470
Joined: 09 May 2003 Posts: 25783 Location: The netherlands
|
Posted: Tue Oct 23, 2018 7:02 am Post subject: |
|
|
both. Destroy calls terminate() itself and then calls .waitfor() and waits till it's finished
You can also call .waitfor() yourself
_________________
Do not ask me about online cheats. I don't know any and wont help finding them.
Like my help? Join me on Patreon so i can keep helping |
|
Back to top |
|
 |
zxuiji Advanced Cheater
Reputation: 1
Joined: 26 Sep 2016 Posts: 70
|
Posted: Tue Oct 23, 2018 8:43 am Post subject: |
|
|
I'll go with destroy then thanks :)
_________________
Those who do not trust in christ to save them from their sins will only experience hell when their soul is released from it's earthly vessel. |
|
Back to top |
|
 |
zxuiji Advanced Cheater
Reputation: 1
Joined: 26 Sep 2016 Posts: 70
|
Posted: Thu Oct 25, 2018 3:52 am Post subject: |
|
|
CE hangs when I try to destroy my threads, can you or someone else familiar with threads take a look at my code and see if you can identify the cause please?
Code: |
local exec = nil
mr.OnActivate = function(mr,b,c)
exec = true
return true
end
mr.OnDeactivate = function(mr,b,c)
exec = nil
return true
end
local t = createThread(function(self)
local next
local n
local chk
self.Name = text .. ' Thread'
if type(self.Terminated) == 'boolean' then
chk = function() return self.Terminated end
else
chk = self.Terminated
end
while chk() == false do
next = 1
n = 1
if exec then
for i,v in pairs(codes) do
if n >= next then
next = v.handl()
n = 0
else
next = 0
end
if next < 1 then
break
end
n = n + 1
end
end
end
end)
mr.OnDestroy = function(mr)
exec = nil
t.destroy()
end
|
_________________
Those who do not trust in christ to save them from their sins will only experience hell when their soul is released from it's earthly vessel. |
|
Back to top |
|
 |
Dark Byte Site Admin
Reputation: 470
Joined: 09 May 2003 Posts: 25783 Location: The netherlands
|
Posted: Thu Oct 25, 2018 5:58 am Post subject: |
|
|
when you destroy a thread it waits till the thread reaches the end of the code
one reason can be that you havn"t told it to stop yet
the other one is that it uses synchronize while it's been told it has to stop. This is because the "wait till finished' may not call checkSynchronize, so when the thread is waiting for synchronize to finish it will wait forever because the main thread is waiting for the thread to finish (you can get around this by checking if the thread is Finished and if not, call checkSynchronize() and then call destroy() )
_________________
Do not ask me about online cheats. I don't know any and wont help finding them.
Like my help? Join me on Patreon so i can keep helping |
|
Back to top |
|
 |
|