tag:blogger.com,1999:blog-4254062391982392824.post2680273133777742667..comments2023-03-16T22:22:13.652+13:00Comments on Designing Code: Multithreading and LuaKeithhttp://www.blogger.com/profile/08109858095787759082noreply@blogger.comBlogger6125tag:blogger.com,1999:blog-4254062391982392824.post-18240759351758934652011-07-15T01:17:25.781+12:002011-07-15T01:17:25.781+12:00This article might be 3 years old, but it still se...This article might be 3 years old, but it still seems to pop up in a Google search. The better way to do this is through mutex, both explicit and through Lua's lock support:<br /><br />Threads Tutorial<br />http://lua-users.org/wiki/ThreadsTutorial<br /><br />Coroutines are not preemptive multithreading, and locks are always needed when data (the Lua state) is shared between threads.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-4254062391982392824.post-90161741322906695892011-05-29T01:11:50.479+12:002011-05-29T01:11:50.479+12:00I just started looking at Lua last week, and I am ...I just started looking at Lua last week, and I am surprised that it doesn't have multithreading, as its authors don't believe in multithreading. Their coroutines approach is too cumbersome.<br />for this reason, I am not going further with Lua as I use a lot of multithreading in my Java programs, and so far has never had any problem with any of the programs that i write. I am going next to try Ruby, it seems to have both multithreading and some coroutines.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-4254062391982392824.post-24784441776111407172010-10-22T04:07:56.634+13:002010-10-22T04:07:56.634+13:00Lua also supports multithreading very well compare...Lua also supports multithreading very well compared to other scripting languages like Python or Ruby, by allowing you to create multiple instances of the VM/interpreter -- one per thread -- without requiring any global locks of any kind.<br />You can then implement a message passing interface between the different interpreters.Bill McGannnoreply@blogger.comtag:blogger.com,1999:blog-4254062391982392824.post-79479848701166779752010-02-13T10:35:26.970+13:002010-02-13T10:35:26.970+13:00Anonymous: Lua has tools to handle this issue. In ...Anonymous: Lua has tools to handle this issue. In fact, almost the exact problem you brought up was handled <a href="http://www.lua.org/pil/9.4.html" rel="nofollow">in the book</a>.<br /><br />The general answer is pretty simple, have your file access / socket reading / etc functions yield for you until the data is available.Adam Joneshttps://www.blogger.com/profile/11581996527740516702noreply@blogger.comtag:blogger.com,1999:blog-4254062391982392824.post-68807457708668122922010-01-27T14:50:56.922+13:002010-01-27T14:50:56.922+13:00Some things cannot be done at all with Lua's c...Some things cannot be done at all with Lua's cooperative multithreading.<br /><br />Suppose I want to do something interactive in a GUI while I'm loading data from a file.<br /><br />The file operation blocks and... so do every other coroutine, because Lua is single-threaded.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-4254062391982392824.post-14739385299305978212008-10-19T21:34:00.000+13:002008-10-19T21:34:00.000+13:00"...we stillthink that no one can write correct pr..."...we still<BR/>think that no one can write correct programs in a language where<BR/>‘a=a+1’ is not deterministic."<BR/><BR/>I could not disagree more with that statement. Preemptive multithreading has many applications that cannot be completely replaced with coroutines without too much hassle. And saying that with PMT you end up only with undeterministic programs just shows a lack of understanding of synchronization and how to use it correctly.<BR/><BR/>Although I must say that the whole point of PMT is to remove from the programmer the task of scheduling. The CPU is much much better at scheduling than a human will ever be. Programmer-scheduled apps are highly inefficient due to the programmer's natural inability to foresee CPU time consumption. Unless the programmer wastes hours calculating how many millisecond a certain operation will take he will be unable to predict when to suspend a thread with low priority.<BR/>Either he ends up with a thread that abuses the CPU or one that never finishes what it was supposed to do.TestNamehttps://www.blogger.com/profile/06666841893487021218noreply@blogger.com