|
Here's a major problem: how does Lua handle multi-threading?
Here's why:
Script 1 increments an Int Value, and Script 2 reads that same Int Value from time to time. Unfortunately, because that Int Value is always being either updated or read, a dead-lock occurs and the place freezes.
So, I'm trying to avoid that problem, but I don't know how. Something to do with locking variables and unlocking them? I really don't know.
I need help, please - I don't know how to implement these things yet. I'm positivie it is possible. |
|
|
stang90Join Date: 2006-09-03 Post Count: 5633 |
your allways asking questions about scripting, and you see to know alot, why isnt your place good then? |
|
|
I think Chess hasn't been working on his place, and has rather been learning more advanced LUA so that he can accomplish what he wants his place to be eventually. He told me, but I won't post it here in case he wants it to remain a secret. |
|
|
Thanks shanethe13, and I guess to find out more info you can easily check out my place's description - but don't bother trying to go in.
Anyway, the reason I post so many questions (yet 'know' a lot) is simply:
-My topics are advanced (as shanethe13 said)
-There's practically no information on the whole Internet (that is easily found) that says much about Lua, and none that says how to do Lua multithreading problems in Roblox
I happen to be an experienced programmer (not expert, just a few years of experience), but I don't understand all that much about Lua since there isn't any help files to learn from. Just an object browser, and that doesn't even tell you HOW or WHY to use objects.
So, anyone know anything about this problem? OR a way around it? Perhaps if I only access the variable (I'm talking about the 2nd script) every now and then rather than constantly it would work? |
|
stang90Join Date: 2006-09-03 Post Count: 5633 |
so your saying lua.com dosnt have alot of info on lua scripting? |
|
|
You could try making the one script access the variable every second, while making the other access it every 0.5 seconds, or 1.5 seconds. This might solve the your issue if a Deadlock really is the problem. |
|
|
I'm sure lua.com does have a lot, but even multithreading is a rare topic and very advanced. It isn't easy to program, and it usually takes a long time to figure out how to do. The problem comes that if there is a queer sort of deadlock, in my real scripts I won't be timing it necessarily.
I'll try just doing my 2nd script enabled, and see if it changes anything. If it doesn't freeze, then we know it has something to do with a deadlock, and I'll change the code to what you suggested shanethe13. |
|
|
... I just looked at it - there is simply a problem with the 2nd script. It makes sense - the moment it is done with a variable, it allows it to be unlocked (and accessed by other scripts).
I'm now using the other thread, since basically it's the same problem as before. |
|
erik.casselTop 100 PosterJoin Date: 2006-02-27 Post Count: 443 |
Roblox executes all scripting code synchronously. At the moment, a script's execution cannot be pre-empted. This means that there is no way to create a threading deadlock in scripting.
Lua sometimes uses the term "coroutine" instead of "thread".
-Erik
|
|
|
Oh thankyou!
Now I just have to figure out - what is wrong with this code:
while true do
st = "Hello: " .. game.Workspace.ATime.Value
print(st)
end
Any ideas, erik.cassel? |
|
erik.casselTop 100 PosterJoin Date: 2006-02-27 Post Count: 443 |
chess,
If your place is freezing then the problem is likely that your script is running an infite loop. The best way to find out is to pepper your code with print() statements.
-Erik
|
|
|
Well, that would explain it!
OK - that's exactly what's been happening, but I've done it before without problems! I'll show an example in a moment, but currently Roblox wants me to restart... so I will... |
|
|
Ex. of regen script that has infinite loop:
model = game.Workspace.Scenery
messageText = "Regenerating Scenery..."
message = Instance.new("Message")
message.Text = messageText
message2 = Instance.new("Message")
message2.Text = "Regenerating Scenery in 1 minute"
backup = model:clone()
wait(15)
while true do
wait(538)
message2.Parent = game.Workspace
wait(2)
message2.Parent = nil
wait(60) -- regenerate this model every 600 seconds
message.Parent = game.Workspace
model:remove()
wait(3) -- display regen message for 4 seconds
model = backup:clone()
model.Parent = game.Workspace
model:makeJoints()
message.Parent = nil
end
There is an infinite loop in there, so why does that work yet a simple loop doesn't? Does it have to do with the "wait" command? As in, as long as you are waiting for some period of time Lua will exit that script for a while? |
|
|
YES! THAT WORKS!
In other words, Lua will exit a thread when ever you insert a wait command.
I also found out something else: If you pause, it doesn't stop the threads!
I'd suggest trying to adjust that at some point; threads that support sudden cancellation if in an infinite loop (and thus they stop at the next loop or something).
Is there another way of avoiding that problem (not that it's a problem, really)? |
|
|
|