淺談chuck-lua中的多線程
chuck-lua支持actor模式的線程模型.可以通過cthread.new創(chuàng)建線程,然后通過cthread.sendmail向線程發(fā)送消息.
與skynet這種框架不同,chuck-lua并不提供多線程的任務/消息調(diào)度功能,每個線程維護了一個簡單的線程郵箱,用于緩存其它線程發(fā)過來的消息.
下面看一個簡單的多線程服務器示例:
mtserver.lua
local chuck = require("chuck") local engine = require("distri.engine") local socket_helper = chuck.socket_helper local Distri = require("distri.distri") local cthread = chuck.cthread local worker function on_new_client(fd) cthread.sendmail(worker,{fd}) end local fd = socket_helper.socket(socket_helper.AF_INET, socket_helper.SOCK_STREAM, socket_helper.IPPROTO_TCP) socket_helper.addr_reuse(fd,1) local ip = "127.0.0.1" local port = 8010 if 0 == socket_helper.listen(fd,ip,port) then print("server start",ip,port) local server = chuck.acceptor(fd) server:Add2Engine(engine,on_new_client) Distri.Signal(chuck.signal.SIGINT,Distri.Stop) worker = cthread.new("distri/test/worker.lua") Distri.Run() end
worker.lua
local chuck = require("chuck") local socket = require("distri.socket") local engine = require("distri.engine") local clone = chuck.packet.clone local cthread = chuck.cthread local Distri = require("distri.distri") --設置郵件處理函數(shù) cthread.process_mail(engine,function (sender,mail) local fd = table.unpack(mail) local s = socket.stream.New(fd) if s:Ok(4096,socket.stream.decoder.rawpacket(),function (_,msg,errno) if msg then s:Send(clone(msg)) else s:Close(errno) s = nil end end) then s:SetRecvTimeout(5000) else s:Close() end end) Distri.Run()
這個示例很簡單,主線程啟動監(jiān)聽,創(chuàng)建一個線程,當接收到連接時就將fd發(fā)送給worker線程.
在這里需要簡單介紹一下chuck-lua線程相關的一些細節(jié).
因為各線程跑在獨立的虛擬機上,因此無法直接通過消息的方式將一個虛擬機中的對象發(fā)送到另一個線程中.目前sendmail將作為消息傳遞給它的lua table序列化為一種適合傳輸?shù)膶ο?目標線程接收這個對象之后再重新轉(zhuǎn)化成本線程虛擬機中的lua table.目前消息支持lua中的所有基本類型,但為了安全考慮,不支持直接傳遞userdata類型.
用cthread.sendmail向目標線程發(fā)送消息時,如果到達目標郵箱的緩沖上線將會阻塞.所有期望處理郵件消息的線程都必須調(diào)用cthread.process_mail設定消息回調(diào)函數(shù).如果不設定,將可能導致消息發(fā)送線程永久阻塞.
線程使用join模式創(chuàng)建,創(chuàng)建者可以通過cthread.join等待線程的結束.
以上所述就是本文的全部內(nèi)容了,希望大家能夠喜歡。
相關文章
Lua中的loadfile、dofile、require詳解
這篇文章主要介紹了Lua中的loadfile、dofile、require詳解,本文分別用實例講解它的用法和特點等內(nèi)容,需要的朋友可以參考下2014-09-09Lua教程(四):在Lua中調(diào)用C語言、C++的函數(shù)
這篇文章主要介紹了Lua教程(四):在Lua中調(diào)用C語言、C++的函數(shù),本文給出了多個示例講解如何在Lua中調(diào)用C/C++寫的函數(shù),需要的朋友可以參考下2014-09-09Openresty服務器使用lua腳本寫的Hello World簡單實例
這篇文章主要介紹了Openresty服務器使用lua腳本寫的Hello World簡單實例,OpenResty (也稱為 ngx_openresty)是一個全功能的 Web 應用服務器。它打包了標準的 Nginx 核心,很多的常用的第三方模塊,以及它們的大多數(shù)依賴項,需要的朋友可以參考下2015-04-04