Skip to content

Threads

Kevin Zhao edited this page Jan 29, 2018 · 3 revisions

Threads

Threads (or technically, fibers) can be created using the CreateThread function, which requires a LuaFunction argument as the function for the thread to run:

var function = lua.LoadString(@"
    print('test')
    local x = coroutine.yield()
    print('val: ' .. x)
    local y = coroutine.yield()
    print('val 2: ' .. y)");

The resulting thread can then be resumed using the Resume method, so long as CanResume is true.

Dynamic Access

LuaThreads support dynamic access:

dynamic thread = (LuaThread)lua.DoString("return coroutine.create(function() return 4 end)")[0];
Assert.Equal(4L, thread());
Clone this wiki locally