test.cpp
#include <iostream>
#include <lua.hpp>
void testCoroutine()
{
auto L = luaL_newstate();
luaL_openlibs(L);
if (luaL_dofile(L, "coroutine.lua"))
{
std::cout << lua_tostring(L, lua_gettop(L)) << std::endl;
lua_close(L);
return;
}
auto T = lua_newthread(L);
lua_getglobal(T, "func");
while (lua_resume(T, nullptr, 0))
{
std::cout << lua_tostring(T, lua_gettop(T)) << std::endl;
getchar();
}
lua_close(L);
}
int main(int argc, char* argv[])
{
testCoroutine();
return 0;
}
coroutine.lua
function func()
coroutine.yield("A")
coroutine.yield("B")
coroutine.yield("C")
end