Introduction
Who hastoread this
Target audience is th e programmer wishi ng to have multiple Lua universes, ea ch running into a independent OS thread.
The name
We choose the « task » name to avoid confusion with « lua threads ».
The idea
Imagine adata base search system, waiting for requests from clientswith dif ferent communication mechanism: Net bios, TCP/IPand Websphere MQ:
We will ass ume that the Data Base Se rver o utperform ou r needs.
Now ,we can identify at le ast four specific functions inside the syste m:
1. Netbios communications.
2. TCP/IP communications.
3. MQ communications.
4. Search andretrieval
Building
Step 1
Expand src directory from tgz inside src d irectory of y our Lua installati on.
Step 2
Win32
Y ou mustselect the threading support:
1. define NA TV_WIN32 :Original code without cancellati on.
2. not define NA TV_WIN 32 : Pthre ads−Win32 code dependent. (Y ou must have Pthreads−Win32 SNAPSHOT 2004−06−22 or later)
Static
Adapt your LibLuaLib.dsp using build/win32/static/Li bLuaLib.dsp as a n example.
Build l ua.exe
Loadable module
Put build/win32/module/task.ds p inside src/Lu aT ask of your installati on.
Add it to Lua.dsw
Build task. dll
Linux/BSD
Static
Adapt your config using build/ix/static/confi g as anexample.
Adapt your src/lib/M akefile using build/ix/static/M akefile as anexample.
Build l ua binary .
Loadable module
Adapt your config using build/ix/module/co nfig as an example.
Put build/ix/module/M akefile in side src/LuaT ask of yo ur installation.
Build l ibtask.s o
Programming
How to use it
If you static ally link ed LuaT ask, yo u must put acall to luaopen_task( ) after c alling to lua_open( ) in your program. This is th e only thi ng to co de in C language.
If you are using the LuaT ask dynamic library ,you must include the fo llowing code in the main task:
require ‘task’
Initial izati on code inside LuaT ask creates the global tasks l ist,gets athread specific key , and creates the « task » namespace.
Now ,you can use the functions inside « task » namespace to ma nipulate tasks a nd messages.
Look at the « LuaT ask Reference Gui de » for functi ons syntax.
A very simple example with two tasks
1. Amain task s howing aprompt andgetting a command.
2. Asecondary echo task.
The main task (t1.lu a ):
require 'task' local myid = task.id() −− gets the current task id local tsk, err = task.create( 't2.lua', { myid}) −− creates a new task while true do io.stdout:write( '\necho> ') −− shows prompt local cmd = io.stdin.read( '*l') −− gets command if cmd == 'quit' then break −− if comm and is quit, terminates else task.post( tsk, cmd, 0) −− sends command text to ech o task local buf, flags, rc = task.receive( −1) −− waits for ans wer io.stdout:write( buf) −− shows answer end end task.post( tsk, '', 1) −− sends dummy message with "1" as stop fla g task.receive( 1000) −− waits (1 sec) for stop acknowle dge from ech o task The "echo" task ( t2.l ua ): local main_id = arg[1] −− gets the main task id from arg[1] while true do local buf, flags, rc = task.receive( −1) −− waits for message if flags == 1 then break −− if flag s is stop, termina tes else task.post( main_id, 'Echo: {' .. buf .. '}', 0) −− sends echo end end task.post( main_id, '', 1) −− sends acknowledge for stop message
Lua Task 1.6 Manual (438.34 KO) (Cours PDF)