Capricorn 76
This Lua tutorial demonstrates how to using multi-threading with the ScriptENGINE.

Introduction

This simple application uses two independent threads of execution to flood the terminal with the words "main" and "thread", separated by commas.

---------------------------
---------------------------
-- Tutorial: Multi threading test
-- This simple program uses two independent threads of execution to flood the terminal with the words "main" and "thread", separated by commas.
--
-- Copyright (c) 2006-2008 Capricorn 76 Pty. Ltd.
-- Original code and documentation copyright (c) Diego Nehab, lua.org.
---------------------------
---------------------------

---------------------------
-- Load the Lua Lanes module into the engine
---------------------------
require ("lanes");

---------------------------
-- Global function called by both threads
---------------------------
local function flood(word, delay)
    local i;
    for i = 1,100 do
        print(word .. ',' .. i);
        IApp:sleep(delay);
    end
end

---------------------------
---------------------------
-- PROGRAM STARTS HERE
-- All global code (outside of any functions) is first run.
-- Then the ScriptENGINE automatically run the State Machine by looking for the OnStateRun function
---------------------------
---------------------------
function OnStateRun(scriptName, param)
    -- Show the consol window
    IApp:showConsoleWindow();

    -- Create a background thread, to output the word 'parent' to the console
    -- When creating lanes, we can specify libraries to load into the new lane thread. By default no libraries are loaded.
    local threadFunc = lanes.gen("engine,base", flood); -- Created a thread generator. No threads have started yet.

    -- Start a thread
    local a = threadFunc('thread', 20); -- Starts the thread running. Passes the input parameter.

    -- At the same time, let's make the main thread print some output to the console
    flood('main', 5);

    -- Wait for user input
    print('Press any key to exit...');
    IKey:getch();

end

Thread synchronisation using locks

This application synchronises two independent threads of execution using locks.

---------------------------
---------------------------
-- Tutorial: Multi threading test
-- This program uses thread locks to synchronise thread access to shared data.
--
-- Copyright (c) 2006-2008 Capricorn 76 Pty. Ltd.
-- Original code and documentation copyright (c) Diego Nehab, lua.org.
---------------------------
---------------------------

---------------------------
-- Load the Lua Lanes module into the engine
---------------------------
require ("lanes");

local linda = lanes.linda();    -- Create a Linda
local lock_func = lanes.genlock(linda, 'lock'); -- Create a lock in the linda for use by the threads

---------------------------
-- Global function called by both threads
---------------------------
local function flood(word)
    local i, j;
    for i = 1,20 do
        ---------------------------
        -- Attempt to grab the thread lock.
        -- If the other thread currently has the lock, this thread will be suspended until the lock becomes available.
        -- At which time, this thread will then own the lock and continue processing from the next line after the lockGrab()
        ---------------------------
        lock_func(1);

        ---------------------------
        -- This code can only be executed by one thread at a time, because of the use of thread locks.
        -- You'll notice in the output that we see groups of "main1, main2, main3, main4, main5" OR "thread1, thread2, thread3, thread4, thread5".
        ---------------------------
        for j = 1,5 do
            print(word .. ', ' .. j);
        end

        ---------------------------
        -- Release the thread lock.
        -- If the other thread is suspended, waiting for this lock to become available, it will automatically be reactivated.
        ---------------------------
        lock_func(-1);
    end
end

---------------------------
---------------------------
-- PROGRAM STARTS HERE
-- All global code (outside of any functions) is first run.
-- Then the ScriptENGINE automatically run the State Machine by looking for the OnStateRun function
---------------------------
---------------------------
function OnStateRun(scriptName, param)
    -- Show the consol window
    IApp:showConsoleWindow();

    -- Create a background thread, to output the word 'parent' to the console
    local threadFunc = lanes.gen("base", flood);    -- Created a thread generator. No threads have started yet.

    -- Start a thread
    local a = threadFunc('thread'); -- Starts the thread running. Passes the input parameter.

    -- At the same time, let's make the main thread print some output to the console
    flood('main');

    -- Wait for user input
    print('Press any key to exit...');
    IKey:getch();
end

Copyright © 2006-23 Sep 2009 Capricorn 76 Pty. Ltd. (created on Wed Sep 23 16:49:11 2009)