Difference between revisions of "ThreadApi"

From Vendetta Lua
Jump to: navigation, search
(New page: This is the API to VO's thread functionality. Threads are actually coroutines and VO wraps this API around them. Thread.Create(threadfunc, ...) -- returns threadhandle or nil,err Threa...)
 
(Added GeSHi highlighting.)
 
Line 1: Line 1:
 
This is the API to VO's thread functionality. Threads are actually coroutines and VO wraps this API around them.
 
This is the API to VO's thread functionality. Threads are actually coroutines and VO wraps this API around them.
 
+
<source lang="lua">
Thread.Create(threadfunc, ...)  -- returns threadhandle or nil,err
+
Thread.Create(threadfunc, ...)  -- returns threadhandle or nil,err
Thread.SendMessage(threadhandle, messageid, argumenttable)  -- returns coroutine.resume error if any
+
Thread.SendMessage(threadhandle, messageid, argumenttable)  -- returns coroutine.resume error if any
Thread.WaitForSingleMessage(threadhandle, messageid)  -- returns arguementtable
+
Thread.WaitForSingleMessage(threadhandle, messageid)  -- returns arguementtable
Thread.WaitForMultipleMessages(threadhandle, {messageid,messagename,messagename,...})  -- returns messageid, argumenttable
+
Thread.WaitForMultipleMessages(threadhandle, {messageid,messagename,messagename,...})  -- returns messageid, argumenttable
Thread.WaitForAnyMessage(threadhandle)  -- returns messageid, argumenttable
+
Thread.WaitForAnyMessage(threadhandle)  -- returns messageid, argumenttable
Thread.PeekMessage(threadhandle, removemsg)  -- returns nil if no message in mailbox or messageid, argumenttable and if removemsg is true, remove the message
+
Thread.PeekMessage(threadhandle, removemsg)  -- returns nil if no message in mailbox or messageid, argumenttable and if removemsg is true, remove the message
Thread.WaitForCondition(cond)  -- returns nothing
+
Thread.WaitForCondition(cond)  -- returns nothing
Thread.SignalCondition(cond)  -- returns nothing
+
Thread.SignalCondition(cond)  -- returns nothing
 
+
</source>
 
Basic samples of how to use these functions:
 
Basic samples of how to use these functions:
 +
<source lang="lua">
 +
local SomeMessageID1 = "SomeMessageName"
 +
local SomeMessageID2 = 10
 +
 +
function threadfunc(mythreadhandle, ...)
 +
    local threadarg1 = arg[1]
 +
    local threadarg2 = arg[2]
  
local SomeMessageID1 = "SomeMessageName"
+
    local msg,msgargs = Thread.WaitForMultipleMessages(mythreadhandle, {SomeMessageID1, SomeMessageID2})
local SomeMessageID2 = 10
+
    if msg == SomeMessageID1 then
 +
        -- do stuff
 +
    elseif msg == SomeMessageID2 then
 +
        -- do other stuff
 +
    end
 +
end
 
   
 
   
function threadfunc(mythreadhandle, ...)
+
somethread = Thread.Create(threadfunc, threadarg1, threadarg2)
local threadarg1 = arg[1]
+
Thread.SendMessage(somethread, SomeMessageID1, "stuff")
local threadarg2 = arg[2]
+
</source>
+
local msg,msgargs = Thread.WaitForMultipleMessages(mythreadhandle, {SomeMessageID1, SomeMessageID2})
+
if msg == SomeMessageID1 then
+
-- do stuff
+
elseif msg == SomeMessageID2 then
+
-- do other stuff
+
end
+
end
+
+
somethread = Thread.Create(threadfunc, threadarg1, threadarg2)
+
Thread.SendMessage(somethread, SomeMessageID1, "stuff")
+
 
+
  
 
Messages are queued in the thread's mailbox if the thread isn't waiting for that particular message and the Wait functions will automatically return if called to wait for a queued message.
 
Messages are queued in the thread's mailbox if the thread isn't waiting for that particular message and the Wait functions will automatically return if called to wait for a queued message.

Latest revision as of 22:06, 23 September 2007

This is the API to VO's thread functionality. Threads are actually coroutines and VO wraps this API around them.

Thread.Create(threadfunc, ...)  -- returns threadhandle or nil,err
Thread.SendMessage(threadhandle, messageid, argumenttable)  -- returns coroutine.resume error if any
Thread.WaitForSingleMessage(threadhandle, messageid)  -- returns arguementtable
Thread.WaitForMultipleMessages(threadhandle, {messageid,messagename,messagename,...})  -- returns messageid, argumenttable
Thread.WaitForAnyMessage(threadhandle)  -- returns messageid, argumenttable
Thread.PeekMessage(threadhandle, removemsg)  -- returns nil if no message in mailbox or messageid, argumenttable and if removemsg is true, remove the message
Thread.WaitForCondition(cond)  -- returns nothing
Thread.SignalCondition(cond)  -- returns nothing

Basic samples of how to use these functions:

local SomeMessageID1 = "SomeMessageName"
local SomeMessageID2 = 10
 
function threadfunc(mythreadhandle, ...)
    local threadarg1 = arg[1]
    local threadarg2 = arg[2]
 
    local msg,msgargs = Thread.WaitForMultipleMessages(mythreadhandle, {SomeMessageID1, SomeMessageID2})
    if msg == SomeMessageID1 then
        -- do stuff
    elseif msg == SomeMessageID2 then
        -- do other stuff
    end
end
 
somethread = Thread.Create(threadfunc, threadarg1, threadarg2)
Thread.SendMessage(somethread, SomeMessageID1, "stuff")

Messages are queued in the thread's mailbox if the thread isn't waiting for that particular message and the Wait functions will automatically return if called to wait for a queued message.