Using LuaTask

Cours Using LuaTask, tutoriel & guide de travaux pratiques en pdf.

Building
Step 1
Expand src directory from tgz inside src directory of your Lua installation.
Step 2
Win32
You must select the threading support:
1. define NATV_WIN32 : Original code without cancellation.
2. not define NATV_WIN32 : Pthreads−Win32 code dependent. (You must have Pthreads−Win32 SNAPSHOT 2004−06−22 or later)
Static
Adapt your LibLuaLib.dsp using build/win32/static/LibLuaLib.dsp as an example. Build lua.exe
Loadable module
Put build/win32/module/task.dsp inside src/LuaTask of your installation. Add it to Lua.dsw
Build task.dll
Linux/BSD
Static
Adapt your config using build/ix/static/config as an example.
Adapt your src/lib/Makefile using build/ix/static/Makefile as an example. Build lua binary.
Loadable module
Adapt your config using build/ix/module/config as an example.
Put build/ix/module/Makefile inside src/LuaTask of your installation. Build libtask.so
Programming
How to use it
If you statically linked LuaTask, you must put a call to luaopen_task( ) after calling to lua_open( ) in your program. This is the only thing to code in C language.
If you are using the LuaTask dynamic library, you must include the following code in the main task:
require ‘task’
Initialization code inside LuaTask creates the global tasks list, gets a thread specific key, and creates the « task » namespace.
Now, you can use the functions inside « task » namespace to manipulate tasks and messages.
Look at the « LuaTask Reference Guide » for functions syntax.
A very simple example with two tasks
1. A main task showing a prompt and getting a command.
2. A secondary echo task.
The main task ( t1.lua ):
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 command is quit, terminates
else
task.post( tsk, cmd, 0) −− sends command text to echo task local buf, flags, rc = task.receive( −1) −− waits for answer io.stdout:write( buf) −− shows answer
end
end
task.post( tsk,  », 1) −− sends dummy message with « 1 » as stop flag task.receive( 1000) −− waits (1 sec) for stop acknowledge from echo task
The « echo » task ( t2.lua ):
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 flags is stop, terminates
else
task.post( main_id, ‘Echo: {‘ .. buf .. ‘}’, 0) −− sends echo
end
end
task.post( main_id,  », 1) −− sends acknowledge for stop message

Cours gratuitTélécharger le cours complet

Télécharger aussi :

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *