ThreadApi

From Vendetta Lua
Revision as of 19:42, 21 September 2007 by Raybondo (Talk | contribs) (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...)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

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.