<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
	<channel>
		<title>Форум QUIK [тема: Как в lua api c++, приостановить выполнение функции через определенное время.]</title>
		<link>http://forum.quik.ru</link>
		<description>Новое в теме Как в lua api c++, приостановить выполнение функции через определенное время. форума  на сайте Форум QUIK [forum.quik.ru]</description>
		<language>ru</language>
		<docs>http://backend.userland.com/rss2</docs>
		<pubDate>Sun, 26 Apr 2026 18:01:23 +0300</pubDate>
		<item>
			<title>Как в lua api c++, приостановить выполнение функции через определенное время.</title>
			<description><![CDATA[<b><a href="http://forum.quik.ru/messages/forum10/message39324/topic4690/">Как в lua api c++, приостановить выполнение функции через определенное время.</a></b> <i>Lua api</i> в форуме <a href="http://forum.quik.ru/forum10/">Программирование на языке Lua</a>. <br />
			Здравствуйте, уважаемые программисты.<br />Есть вопрос &nbsp;как в lua api c++, приостановить выполнение функции через определенное время.<br /><br />Обычно приостановление функций, если lua_resume вернула yield.<br /><br />Как тут для примера.<br /><br />
====code====
<pre>
int my_yield(lua_State* L) {
&nbsp;&nbsp;&nbsp;return lua_yield(L, 0);
};
const char* LUA = R"(
function foo()
print(" func foo &#92;n")
end

function main()
for i = 1, 3 do
print(" func main "..i.."&#92;n")
my_yield()
end&nbsp;&nbsp;end
)";
int main(int argc, char* argv&#91;&#93;) {
&nbsp;&nbsp;&nbsp;lua_State* L = luaL_newstate();/*Функция создает новое Lua состояние. Она вызывает lua_newstate с функцией-*/
&nbsp;&nbsp;&nbsp;luaL_openlibs(L);
&nbsp;&nbsp;&nbsp;lua_State* L1 = lua_newthread(L);
&nbsp;&nbsp;&nbsp;cout &#60;&#60; "&#92;n";
&nbsp;&nbsp;&nbsp;lua_register(L, "my_yield", my_yield);
&nbsp;&nbsp;&nbsp;checkerror(L, LUA);
&nbsp;&nbsp;&nbsp;lua_getglobal(L, "main");
&nbsp;&nbsp;&nbsp;while (true) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int ret = lua_resume(L, L1, 0);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (ret == LUA_YIELD) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lua_getglobal(L1, "foo");
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lua_pcall(L1, 0, 0, 0);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else if (ret == 0) {

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;};

&nbsp;&nbsp;&nbsp;lua_close(L);
&nbsp;&nbsp;&nbsp;return 0;
};

</pre>
=============
<br /><br /><br />Функция main вызывает функцию my_yield и функция main приостановиться уступая функции foo.<br /><br />Как это сделать без вызова функции my_yield?<br />
====code====
<pre>
int my_yield(lua_State* L) {
&nbsp;&nbsp;&nbsp;this_thread::sleep_for(chrono::milliseconds(306));// задержка
&nbsp;&nbsp;&nbsp;return lua_yield(L, 0);
};

const char* LUA = R"(
function foo()
print(" func foo &#92;n")
end

function main()
while true do

print(" main &#92;n")
end&nbsp;&nbsp;end
)";
int main(int argc, char* argv&#91;&#93;) {
&nbsp;&nbsp;&nbsp;lua_State* L = luaL_newstate();/*Функция создает новое Lua состояние. Она вызывает lua_newstate с функцией-*/
&nbsp;&nbsp;&nbsp;luaL_openlibs(L);
&nbsp;&nbsp;&nbsp;lua_State* L1 = lua_newthread(L);
&nbsp;&nbsp;&nbsp;checkerror(L, LUA);
&nbsp;&nbsp;&nbsp;lua_getglobal(L, "main");// получить функцию.&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;while (true) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;thread th(my_yield, std::ref(L)); //th.detach();// независимый поток.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int ret = lua_resume(L, L1, 0);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout &#60;&#60; ret &#60;&#60; endl;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (ret == LUA_YIELD) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lua_getglobal(L1, "foo");
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lua_pcall(L1, 0, 0, 0);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else if (ret == 0) {/*Когда функция lua_resume возвращается, стек содержит все значения,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;переданные в lua_yield, или все значения, возвращенные телом функции.*/

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;};
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;lua_close(L);
&nbsp;&nbsp;&nbsp;return 0;
};
</pre>
============= <br />
			<i>15.08.2019 15:18:52, Егор.</i>]]></description>
			<link>http://forum.quik.ru/messages/forum10/message39324/topic4690/</link>
			<guid>http://forum.quik.ru/messages/forum10/message39324/topic4690/</guid>
			<pubDate>Thu, 15 Aug 2019 15:18:52 +0300</pubDate>
			<category>Программирование на языке Lua</category>
		</item>
	</channel>
</rss>
