• Feeds

  • Lua coroutine 不一样的多线程编程思路

    上周末开始看《Lua程序设计》第二版,目前体会到其中比较有趣的有两点,一是强大的table数据结构,另外就是coroutine。也许Lua中的coroutine是一种很好的设计模式,但我初步的体会还是没想到其他语言和场合能非常适合用到coroutine的场景。

    一、简介

    协同程序与线程差不多,也就是一条执行序列,拥有自己独立的栈,局部变量和指令指针,同时又与其它协同程序共享全局变量和其它大部分东西。线程与协同程序的主要区别在于,一个具有多线程的程序可以同时运行几个线程,而协同程序却需要彼此协作地运行。就是说,一个具有多个协同程序的程序在任何时刻只能运行一个协同程序,并且正在运行的协同程序只会在其显示地挂起时,它的执行才会暂停。

    如:

    co = coroutine.create(function ()
    for i=1,10 do
    print("co", i)
    coroutine.yield()
    end
    end)

    从主线程调用
    coroutine.resume(co)
    会依次打印1到10

    二、原理探析

    • coroutine创建的所谓的“线程”都不是真正的操作系统的线程,实际上是通过保存stack状态来模拟的。
    • 由于是假的线程,所以切换线程的开销极小,同时创建线程也是轻量级的,new_thread只是在内存新建了一个stack用于存放新coroutine的变量,也称作lua_State

    LUA_API lua_State *lua_newthread (lua_State *L)

    • 调用yield()当前线程交出控制权,同时还可以通过stack返回参数。调用resume的线程(可理解为主线程)获得返回的参数。
    • Lua yield()和Java中的Thread.yield()有点相似,但是区别更大。Java中的yield调用后只是将当前CPU切换到另外一个线程,CPU可能随时会继续回到线程执行。
    • 我更倾向于把Lua中的yield()和resume()和Java中的wait()和notify()来对比。它们表现的行为基本一致。
    • 关于stack实现也可参看Yufeng(Erlang高手)的分析文章 lua coroutine是如何实现的?

    三、Why coroutine?

    上面对coroutine有个基本的了解,因此大家都会象我一样去想,为什么要用coroutine?先研究下优点

    • 每个coroutine有自己私有的stack及局部变量。
    • 同一时间只有一个coroutine在执行,无需对全局变量加锁。
    • 顺序可控,完全由程序控制执行的顺序。而通常的多线程一旦启动,它的运行时序是没法预测的,因此通常会给测试所有的情况带来困难。所以能用coroutine解决的场合应当优先使用coroutine。

    再看缺点,研究coroutine缺点之前,我寻找了一下Lua中为什么实现coroutine的一些说明。在巴西人写的paper Coroutines in Lua(pdf)中解释了几个原因:

    • Lua是ANSI C实现的,ANSI C并不包含thread的实现,因此如果要在Lua增加thread的支持就要使用操作系统本地的实现,这样会造成通用的问题。同时也会使Lua变得臃肿。因此Lua选择了在ANSI C上实现的coroutine。
    • Lua主要设计目的之一是给C调用,如果Lua内部又有多线程实现的话会造成C调用状态的混乱,而只提供coroutine层面的挂起则可以保持状态的一致性。

    以上这些理由都是基于Lua特殊的原因而使用的,并不是很通用的原因。我们也了解到,coroutine实际上是一种古老的设计模式,它在60年代就已经定型,但是现代语言很少有重视这个特性,目前可以举例的有Windows的fibers, Python的generators

    四、Lua coroutine和Erlang

    上面优点有1条没展开,就是每个coroutine有自己私有的stack及内存变量空间。因此可以认为coroutine和Erlang中的process是非常相似的。但是coroutine只能同时只有一个在执行,如果能让他多个同时跑,我觉得就和Erlang非常相似了。

    《Lua程序设计》第二版30.2介绍的一种实现方法,让多个c threads启动,然后每个c thread启动一个coroutine(类似Erlang process),然后通过stack传递变量值(类似Erlang process message),这样就可以实现一个类似Erlang的process模型了。由于coroutine实际上可以用任何语言实现,那其他语言应该也可实现同样这种设计方法。

    五、Lua其他

    Lua目前主要用在游戏编程领域,通常的观点Lua是“胶水语言”。用来把各个模块化的功能粘合起来。就我目前阅读的一些代码来看,C和Lua通常是混合在一起的,并没有明确的边界。对于我一个外行的眼光看来我分不清哪些是在做C的事情,哪些是在调用Lua。特别是这个“胶水”如果放得太多,系统中各个模块的独立性将会受到影响。比如云风的这篇Lua 不是 C++也提到,“这属于过厚的粘合层,是绝对需要抛弃的”。

    另外Code@Pig一篇[网游设计] 一点感想也提到要简化调用,我总结它的观点主要两点:

    1. 不要存在冗余的关系,给一个部分负责管理就好。(由Lua/python来管理)
    2. 粘合层(Lua/python接口)不要过胖,我们可以通过引入一个“间接层”来把粘合层做“薄”

    虽然Lua的高效和精简的设计让人赞誉有加,但是它的性能排名并不高,和Python大致在同一个级别。另外“胶水语言”的定位也妨碍了它在更多领域的发展。

    如想及时阅读Tim Yang的文章,可通过页面右上方扫码订阅最新更新。

    « | »

    37 Comments  »

    1. Dimiter Stanev

      Hi Tim,

      With 32-bit lua-5.1.4 cygwin compiled I’m getting on my machine:
      elapsed time: 29.72

      but with 32-bit luajit (www.luajit.org) I’m getting:
      elapsed time: 7.06

      Cheers!

    2. Hey I am new here. I’m sorry for any reason if this this is the wrong place for thispost but I hope If some one here on timyang.net would be able to
      assist me to choose the better choice. The forums here are absolutely amazing and certainly plan on sticking around for as long as I am welcome.

    3. sw

      我觉得协程最大的应用是取代回调机制,当需要异步执行的时候,启动一个协程,然后当前协程挂起去处理其他事务。异步结果完成后再继续执行,看上去是阻塞但实际上不会影响其他应用,另外一个作用是实现复杂跌代器。

    4. db

      其实古老的、普及的VB就有coroutine的变种,而且应用的还很广泛。

    5. Zongjie Hu

      我觉得这个和python的twisted框架很像,应该算是一种异步框架。

    6. lzr

      我觉得你说的都是别人的东西,确没有自己的理解。

    7. I have read so many articles or reviews concerning the blogger lovers however this paragraph is truly a nice post, keep it up.

    8. При необходимости, каждый посетитель официального сайта может вывести на экран симуляторы конкретного провайдера, воспользовавшись системой
      фильтрации.

    9. |Meus parentes cada vez mais dizem que eu estou matando meu tempo aqui na web, porém
      eu sei que estou adquirindo conhecimento, diariamente leio textos muito
      bons como este e já ganho meu dia. http://mailtouch.co.kr/sir/bbs/board.php?bo_table=pcphone_help&wr_id=393803

    10. | Como vai ? . Conheci seu site por meio de um link publicado
      no facebook e adorei . Estarei compartilhando este link para meus seguidoress
      . Grande abraço | eu adoro quando encontro com materiais tão bem redigidos quanto
      o que pude encontrar aqui neste blog . Continuarei visitando esta página já que adorei dos seus textos
      . http://hdxwill.com/en/bbs/board.php?bo_table=free&wr_id=464051

    11. |Estou gostando dos seus artigos, eu acompanho Teu web
      site a algum tempo, Parabéns pela iniciativa de falar sobre este tema conteúdo
      de qualidade. Coloquei Seu web site nos meus favoritos.
      Gratidão! :-) http://greenparking.co.kr/bbs/board.php?bo_table=free&wr_id=40331

    12. we’ll check out your hompage when i know 토토사이트,
      토토사이트 추천, 토토사이트 목록, 안전 토토사이트

    13. We are operating challenging to be the very best Lesbian Milf Teen Videos web page on the web!
      MILF Tube, Mature MILFs, Busty Ebony Mom with Hairy Pussy, Best Amateur Fuck Videos.

      Watch and delight in free of charge ebony porn pictures with black mature gals, lesbians
      and teenagers fuck, huge tits and hardcore sexual intercourse.
      Amateurs of wild and nuts fucking, they are fond of
      anal and oral camara Sex live,
      enjoy masturbation and genuinely into some group more mature sexual intercourse.

      The threatening to distribute, or threatening to capture, an intimate
      impression offences are contained in s. Not all cyber relationship sites even so are
      produced equal. Other consumers have unique preferences that aren’t catered to by regular cam
      web pages. Chaturbate is a single of the most common camming web
      pages in the globe, making it a benchmark in the camming sector.
      The greatest MILF porno site is the 1 with a higher sum of
      Hd videos and frequent updates. You can verify it day by day for the freshest updates and do not skip the ideal information manufactured
      by the grownup business. Fresh updates will never ever depart you unsatisfied.
      Carrie discovered the reputation is why she requested for a plug on Ask Enid, soon after
      which Seema urges her to negotiate that if she writes for Vivante she
      will get a plug.

    14. However, this strategy might weed out folks with useful expertise who should not have the endurance for lengthy doctrinal debates.
      Match-with a purpose to-peer networks, which may be most well-liked among
      small institutions with out a discussion board, merely carry out at any time when each of the pc techniques can be found and likewise connected to the web.
      The Small Practitioners’ Forum is proud to support the CPA Education Foundation and to date have contributed greater than $120,000 to
      the Calgary CPA Small Practitioners Forum Award.
      The greatest asset a successful private shopper can have is nice taste.
      For personal consideration it’s also possible to be a part of a paid membership
      site such as the Affiliate Power Group. Applying protected
      distant control entry programs similar to personal particular person networks will enable employees members acquire entry to information from
      notebook computer systems as soon as they are not actually at
      the job. Launching some form of machine-based mostly
      community retains particulars organized and in addition obtainable
      to people that need it to understand dependableness
      and stability than fellow-to assist-equal cpa networks and random strategies
      of maintaining data. Small enterprise personnel must simply and quickly
      observe down the small print wanted no matter their whereabouts.

    15. With thanks, An abundance of postings!

    16. Ayrıca 7Slots’ta oynamaya başlamanın başka bir yolu daha var – Facebook, Twitter, Gmail ve hatta LinkedIn gibi sosyal medya hesaplarınızı kullanarak kaydolmak.

    17. Yakın çekim klibimiz için, bir dizi malzeme üzerinde gölgelerin zaman atlamasıyla oynamamıza olanak tanıyacak bir açı seçtik.

    18. Bollywood on line casino промокод bbonus открывает двери к насыщенному миру азартных развлечений, предоставляя доступ к специальным бонусам, которые предназначены специально для улучшения игрового опыта.

    19. Здесь на самом деле можно найти автоматы, которые дают чаще других.

    20. Lulu

      The forums here are absolutely amazing and certainly myWarBible plan on sticking around for as long as we are welcome.

    21. If you are going for best contents like myself, just pay a
      visit this web page everyday as it provides feature contents, thanks

    22. Посетители смогут активировать welcome bonus, участвовать в турнирах и розыгрышах денежных призов.

    23. Это позволяет игрокам получать дополнительные бонусы и повышать свои шансы на выигрыш.

    24. На этом процедура регистрации закончена, можно входить в личный кабинет по своим реквизитам.

    25. Are you trying to find a way to improve your online search engine ranking?

      my homepage; https://www.facebook.com/serverifiedlists/

    26. They call me Will Campuzano. To cycle is something she certainly not give awake.
      My wife and I live in New Jersey. Filing is my profession but I’ve already sent applications for another a specific.

    27. Cùng theo dõi tiếp bài viết để xem hướng
      dẫn DA88 và chơi dễ dàng hơn nhé.
      Nhà Cái DA88 là nhà cung cấp Cổng trò chơi, Games bài đổi thưởng, Live casino và Dịch vụ dữ liệu thể thao hàng đầu trong ngành.
      Chơi cá cược, game bài an toàn, kết bảo mật thông tin cá nhân của người chơi tuyệt đối.

      Trên đây là những thông tin cơ bản về các chương trình khuyến mãi QH88.
      Cơ hội làm giàu này chắc hẳn không anh em nào muốn bỏ lỡ.
      UAE cũng có 4 điểm ở bảng C sau 1 trận thắng và 1 trận hòa, nhưng họ đoạt vé nhờ hơn Palestine về hiệu
      số. Màu sắc hài hòa, bố cục rõ ràng, logic giúp người chơi dễ dàng thao tác tìm kiếm game,
      đăng ký, đăng nhập. Ngoài các bác sĩ Việt Nam giỏi, Bệnh viện Thu Cúc còn hợp tác
      với đội ngũ bác sĩ điều trị ung thư nổi tiếng Singapore, nhằm điều trị trực
      tiếp cho người bệnh ung thư tại Việt Nam. Hệ thống đang sử dụng các chương trình và thuật toán phức
      tạp nhằm đơn giản hóa thao tác.

    28. You made your point extremely effectively.!

    29. Welcome to Flexy SMS, your trusted partner for affordable and efficient
      SMS solutions. We are experts in providing low-cost SMS services, SMS
      gateway, and bulk SMS options designed to meet your
      business needs. Whether you’re looking to send advertisement
      messages, updates, or reminders, our reliable SMS gateway ensures fast
      and safe delivery.

      At FlexySMS, we understand the value of smooth communication. Our platform is designed to
      be intuitive, scalable, and highly reliable, ensuring that your messages reach the
      intended recipients without any problems. Leverage our SMS gateway to improve customer engagement,
      enhance marketing campaigns, and streamline your communication processes.

      Join the numerous businesses that trust FlexySMS for their SMS needs
      and enjoy the difference of a service that’s both affordable and effective.
      Discover the power of FlexySMS and take your business communication to the next level.

    30. Hello and welcome. I’m Santo Cifuentes. Maryland is your biggest I love most.
      Fishing is the thing she loves most involving most.

      She works as a payroll clerk but she plans on changing in which.

    31. I am regular reader, how are you everybody? This paragraph posted at this web page is truly good.

    32. I have read so many articles on the topic of
      the blogger lovers however this post is truly a good
      paragraph, keep it up.

    Leave a Comment