線程模塊(luathread.dll)
新線程池創(chuàng)建,可以指定啟動一個或多個線程
thread.new('線程入口腳本文件', 線程數(shù)量);
會根據(jù)線程數(shù)量創(chuàng)建多個線程,并且分別初始化每個線程的lua運(yùn)行環(huán)境,并執(zhí)行指定的線程腳本。
tlist = thread.newlist('隊列類型', '隊列名稱');
創(chuàng)建一個同步隊列,返回隊列對象,此對象必須可以在多個線程中共享的,而且相同名稱的對象
只能存在一個。
tlist:create_new_object();
創(chuàng)建新的對象,次對象可以加入到請求隊列中
tlist:push_request(obj);
將通過create_new_object()創(chuàng)建的對加入到隊列中
tlist:wait_for_req(timeout);
等待隊列中有可以處理的請求
tlist:get_request();
從請求隊列中取出一個請求來進(jìn)行數(shù)據(jù)處理
tlist:get_wait_object();
返回可等待對象,以便腳本可以執(zhí)行多個對象的等待任務(wù)
tlist:get_info();
獲得隊列的一些相關(guān)信息
線程代碼
線程1:
local tlist = thread.newlist('test', 'test0');
local d = tlist:create_new_object();
d:setdata('test', 'hello world');
tlist:push_request(d);
線程2:
local tlist = thread.newlist('test', 'test0');
while(tlist:wait_for_request(1000)) do
local d = tlist:get_request();
handlesomething(d);
end