Multithreading Lua 5.2.2断裂的螺纹系统
哎, 我正在用lua制作线程系统,当我恢复很多线程时,我崩溃了。。。 我是C Lua的noob,我真的不知道在接下来的代码中我在做什么是好是坏 我用“//可能在这里”标记了坠机地点Multithreading Lua 5.2.2断裂的螺纹系统,multithreading,crash,lua,system,coroutine,Multithreading,Crash,Lua,System,Coroutine,哎, 我正在用lua制作线程系统,当我恢复很多线程时,我崩溃了。。。 我是C Lua的noob,我真的不知道在接下来的代码中我在做什么是好是坏 我用“//可能在这里”标记了坠机地点 #包括 #包括 外部“C”{ void uu stdcall Sleep(无符号长); }; int main(){ lua_State*L=luaL_newstate(); luaL_openlibs(L); 卢厄图书馆(L); lua_State*T=lua_新线程(L); luaL_加载文件(T,“c:/clu
#包括
#包括
外部“C”{
void uu stdcall Sleep(无符号长);
};
int main(){
lua_State*L=luaL_newstate();
luaL_openlibs(L);
卢厄图书馆(L);
lua_State*T=lua_新线程(L);
luaL_加载文件(T,“c:/clua.lua”);
文书主任(T);
做{
睡眠(1);
}while(co_update());
卢厄关闭(L);
获取(新字符[1]);
返回0;
};
\ifndef ALA_LUA_H
#定义ALA_LUA_H
#包括
无效luaA_libs(lua_州*);
静态int luaA_睡眠(lua_状态*句柄){
co_sleep(把手,lua_tointeger(把手,1));
返回0;
};
静态int luaA_rthread(lua_状态*句柄){
if(lua_isthread(handle,1)){
co_resume(lua_tothread(handle,1));
};
返回0;
};
静态int luaA_mthread(lua_State*句柄){
if(lua_isfunction(handle,1)){
lua_State*thread=lua_newthread(句柄);
lua_值(手柄,1);
lua_xmove(手柄,螺纹,1);
返回1;
};
返回0;
};
无效luaA_库(lua_状态*句柄){
lua_寄存器(句柄,“mthread”,luaA_mthread);
lua_寄存器(手柄,“rthread”,luaA_rthread);
lua_寄存器(句柄,“sleep”,luaA_sleep);
};
#恩迪夫
\ifndef ALA\u LUA\u CO\H
#定义ALA_LUA_CO_H
#包括
#包括
#包括
#包括
#包括
标准:vectorco_螺纹;
std::vectorco_线程_延迟;
静态常量大小\u t CO\u NPOS=-1;
bool co_update();
新尺寸(卢奥州*);
尺寸(卢厄州*);
无效co_简历(lua_州*);
作废co_报告(lua_州*);
无效co_rem(lua_状态*,尺寸t);
无效co_睡眠(lua_状态*,时间t);
无效副本(lua_州*,尺寸t);
void co_remifdead(lua_State*句柄,大小索引=co_NPOS){
开关(lua_状态(手柄)){
案例LUA_OK:{
if(lua_gettop(handle)=0)co_rem(handle,index);
返回;
};
案例LUA_ERRRUN:{
co_rem(手柄、索引);
返回;
};
};
};
无效co_rem(lua_状态*句柄,大小索引=co_NPOS){
如果(指数==公司利润){
索引=co_get(句柄);
};
如果(索引!=CO_NPO){
co_threads.erase(co_threads.begin()+索引);
删除[]co_线程_延迟[索引];
co_threads_delay.erase(co_threads_delay.begin()+索引);
};
};
布尔公司更新(){
if(co_threads.empty())返回false;
size_t i=co_threads.size();
而(i>0){
--一,;
lua_State*handle=co_线程[i];
if(lua_状态(句柄)=lua_收益率){
如果(*co_threads_delay[i]Lua在操作系统级别没有内置的多线程支持。Lua线程是协程,而不是操作系统线程。您不能在不同的操作系统线程中使用相同母状态的Lua线程。您可以在不同的操作系统线程中使用单独的Lua状态,但必须手动管理两个状态之间的任何数据交换
另一方面,您可以通过定义锁定和解锁宏来构建Lua以支持操作系统级多线程处理,但每次应用程序进入和离开Lua时都会调用这些宏。如果您阅读了我的代码,您应该知道我知道什么是协程…无论如何,感谢您编写的最后一部分…我想我必须做一件事hing grr…你能提供崩溃的回溯吗?没有研究工作,没有错误消息,没有堆栈跟踪。你到底期望什么?这甚至不是一个真正的问题。我的问题是为什么我的程序崩溃…我通过关闭收集垃圾解决了…稍后我将检查如何在不关闭收集垃圾的情况下保存线程
#include<cstdio>
#include<ala_lua.h>
extern"C"{
void __stdcall Sleep(unsigned long);
};
int main(){
lua_State*L=luaL_newstate();
luaL_openlibs(L);
luaA_libs(L);
lua_State*T=lua_newthread(L);
luaL_loadfile(T,"c:/clua.lua");
co_resume(T);
do{
Sleep(1);
}while(co_update());
lua_close(L);
gets(new char[1]);
return 0;
};
#ifndef ALA_LUA_H
#define ALA_LUA_H
#include<ala_lua_co.h>
void luaA_libs(lua_State*);
static int luaA_sleep(lua_State*handle){
co_sleep(handle,lua_tointeger(handle,1));
return 0;
};
static int luaA_rthread(lua_State*handle){
if(lua_isthread(handle,1)){
co_resume(lua_tothread(handle,1));
};
return 0;
};
static int luaA_mthread(lua_State*handle){
if(lua_isfunction(handle,1)){
lua_State*thread=lua_newthread(handle);
lua_pushvalue(handle,1);
lua_xmove(handle,thread,1);
return 1;
};
return 0;
};
void luaA_libs(lua_State*handle){
lua_register(handle,"mthread",luaA_mthread);
lua_register(handle,"rthread",luaA_rthread);
lua_register(handle,"sleep",luaA_sleep);
};
#endif
#ifndef ALA_LUA_CO_H
#define ALA_LUA_CO_H
#include<vector>
#include<time.h>
#include<stdio.h>
#include<cstring>
#include<lua.hpp>
std::vector<lua_State*>co_threads;
std::vector<time_t*>co_threads_delay;
static const size_t CO_NPOS=-1;
bool co_update();
size_t co_new(lua_State*);
size_t co_get(lua_State*);
void co_resume(lua_State*);
void co_report(lua_State*);
void co_rem(lua_State*,size_t);
void co_sleep(lua_State*,time_t);
void co_remifdead(lua_State*,size_t);
void co_remifdead(lua_State*handle,size_t index=CO_NPOS){
switch(lua_status(handle)){
case LUA_OK:{
if(lua_gettop(handle)==0)co_rem(handle,index);
return;
};
case LUA_ERRRUN:{
co_rem(handle,index);
return;
};
};
};
void co_rem(lua_State*handle,size_t index=CO_NPOS){
if(index==CO_NPOS){
index=co_get(handle);
};
if(index!=CO_NPOS){
co_threads.erase(co_threads.begin()+index);
delete[]co_threads_delay[index];
co_threads_delay.erase(co_threads_delay.begin()+index);
};
};
bool co_update(){
if(co_threads.empty())return false;
size_t i=co_threads.size();
while(i>0){
--i;
lua_State*handle=co_threads[i];
if(lua_status(handle)==LUA_YIELD){
if(*co_threads_delay[i]<=clock()){
lua_resume(handle,NULL,0);//here maybe
co_remifdead(handle,i);
};
}else{
co_remifdead(handle,i);
};
};
return!co_threads.empty();
};
void co_resume(lua_State*handle){
switch(lua_status(handle)){
case LUA_YIELD:{
lua_resume(handle,NULL,0);
co_remifdead(handle);
return;
};
case LUA_OK:{
if(lua_gettop(handle)!=0){
size_t index=co_new(handle);
lua_resume(handle,NULL,0);//here maybe
co_remifdead(handle,index);
return;
}else{return;};
};
default:{return;};
};
};
void co_sleep(lua_State*handle,time_t slp){
size_t index=co_get(handle);
if(index!=CO_NPOS){
*co_threads_delay[index]=slp+clock();
lua_yield(co_threads[index],0);
};
};
void co_report(lua_State*handle){
if(lua_status(handle)==LUA_ERRRUN){
const char*error=lua_tostring(handle,-1);
#ifdef ALA_LUA_ERRLOG
FILE*file=fopen(ALA_LUA_ERRLOG,"a");
fputs(error,file);
fclose(file);
#else
puts(error);
#endif
lua_pop(handle,-1);
};
};
size_t co_get(lua_State*handle){
if(co_threads.empty())return CO_NPOS;
const size_t l=co_threads.size();
for(size_t i=0;i<l;++i){
if(co_threads[i]==handle)return i;
};
return CO_NPOS;
};
size_t co_new(lua_State*handle){
if(lua_status(handle)==LUA_OK&&lua_gettop(handle)!=0){
time_t*tm=new time_t[1];
co_threads.push_back(handle);
co_threads_delay.push_back(tm);
return co_threads.size()-1;
};
return CO_NPOS;
};
#endif