Jump to content
  • Advertisement
Sign in to follow this  
hottimbo

LuaThread

This topic is 3914 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I was looking around for multi threading support in Lua, and found LuaThread. It seems to have *real* thread support (unlike coroutines), which means I can use it to leverage a little more performance from multi core machines. Or so I thought. I wrote a simple test which in brief looks like this: function worker(name) -- Do some length maths stuff end start=os.time; thread.newthread(worker,{"A"}); worker("B"); stop=os.time; print(stop-start); Now, I know this isn't perfect, I don't actually wait for thread A to finish, but it will generally run to completion. Ok, heres the thing. If I run worker A and worker B sequentially, it all takes 10 seconds. If I then thread worker A (as above) I would have expected this time to drop (I'm on a dual core machine), but in fact it *increases* to 11 seconds! The same test written in C++ yields a performance gain of around 40% which I would expect. Does anyone have any idea why I'm seeing this behaviour? Tim

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by hottimbo
If I then thread worker A (as above) I would have expected this time to drop (I'm on a dual core machine), but in fact it *increases* to 11 seconds!


The dual core thing means nothing. There's no guarantee that the operating system will decide to place your two threads on different cores, especially when there are probably another 50 threads in memory at that point anyway.

Try with 3, 4, or 5 threads. You may find some of them get put on different cores. Or you may not, and will end up paying the context switching price over the pure sequential version.

Share this post


Link to post
Share on other sites
Ah, this makes sense. What still confuses me though is why my c++ program always shows an increase in performance, and the LuaThread version never does.

I will play around with increasing the thread count.

Thanks.

Share this post


Link to post
Share on other sites
Is there some shared object that both Lua threads access? Such as console output? There could be some hidden locks which slow you down, or even make the threads essentially run sequentially instead of in parallel.

Share this post


Link to post
Share on other sites
There is console output in the worker function, removing it doesnt make any difference. Apart from that the worker function is as simple as can be, it's just to demo LuaThread.

I have included the code here... (sorry, I don't know how to make the code look pretty on this messageboard)


local thread = require("thread");
start = os.time();
local output = thread.newmutex()
local function worker(id)
output:lock()
print(id..": hi!");
output:unlock()
local i = 0;
local result = 0;
while i < 9999999 do
result = result + 10^i;
i = i + 1;
end
output:lock()
print(id..": bye! "..result);
output:unlock()
return
end
thread.newthread(worker, {"threadA"});
worker("slowB");
stop = os.time();
print("Time: " .. stop - start);



I don't see what is making this run sequentially, but it seems that that is what is happening. I do see in windows though that the thread count goes up by 2, so it is creating threads. It's a strange one, but it's putting me off using LuaThread. (That and intermittent crashes :) ).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!