windows lua 多线程 线程同步

简介:

今天在改一个程序,改成部分逻辑用lua写,这个程序是多线程的。将程序中部分逻辑改成lua之后,各种非法访问内存错误,各种奇奇怪怪的问题,不分时间,不分地点的出现崩溃。从调用堆栈来看,基本都是使用lua造成的。在多线程中使用lua_newthread得到的lus_State仍然有时候程序会崩溃。基本上可以确定为多线程中操作lua 的问题了。

前几天我转载的一篇文章,文章写了关于lua多线程的作法。作法有二

1.每一个线程函数用lua_newthread产生一个新的lua_state 以后对lua操作都用这个lua_state

2.修改lua源码使之成为支持多线程的(修改lua 源代码中lua_lock lua_unlock宏)

对于第2条,那篇文中的例子是用 pthread_mutex_t 来进行线程同步的pthread_mutex_t,我对pthread_mutex_t 不熟,网上一查才知道一般是在linux下C语言进行线程同步用的,在windows下面一般多线程都用CRITICAL_SECTION,和内核对象来进行线程同步的。于是产生了这篇文章。

修改代码如下:

1.global_State加一个成员变量  m_cs

Code Snippet
  1. typedef struct global_State {
  2.   stringtable strt;  /* hash table for strings */
  3.   lua_Alloc frealloc;  /* function to reallocate memory */
  4.   void *ud;         /* auxiliary data to `frealloc' */
  5.   lu_byte currentwhite;
  6.   lu_byte gcstate;  /* state of garbage collector */
  7.   int sweepstrgc;  /* position of sweep in `strt' */
  8.   GCObject *rootgc;  /* list of all collectable objects */
  9.   GCObject **sweepgc;  /* position of sweep in `rootgc' */
  10.   GCObject *gray;  /* list of gray objects */
  11.   GCObject *grayagain;  /* list of objects to be traversed atomically */
  12.   GCObject *weak;  /* list of weak tables (to be cleared) */
  13.   GCObject *tmudata;  /* last element of list of userdata to be GC */
  14.   Mbuffer buff;  /* temporary buffer for string concatentation */
  15.   lu_mem GCthreshold;
  16.   lu_mem totalbytes;  /* number of bytes currently allocated */
  17.   lu_mem estimate;  /* an estimate of number of bytes actually in use */
  18.   lu_mem gcdept;  /* how much GC is `behind schedule' */
  19.   int gcpause;  /* size of pause between successive GCs */
  20.   int gcstepmul;  /* GC `granularity' */
  21.   lua_CFunction panic;  /* to be called in unprotected errors */
  22.   TValue l_registry;
  23.   struct lua_State *mainthread;
  24.   UpVal uvhead;  /* head of double-linked list of all open upvalues */
  25.   struct Table *mt[NUM_TAGS];  /* metatables for basic types */
  26.   TString *tmname[TM_N];  /* array with tag-method names */
  27.   CRITICAL_SECTION m_cs/*windows mutithread */
  28. } global_State;

 

2.在lua_newstate加入初始化Critical_section的代码 InitializeCriticalSection(&g->m_cs);

Code Snippet
  1. LUA_API lua_State *lua_newstate (lua_Alloc fvoid *ud) {
  2.   int i;
  3.   lua_State *L;
  4.   global_State *g;
  5.   void *l = (*f)(udNULL, 0, state_size(LG));
  6.   if (l == NULLreturn NULL;
  7.   L = tostate(l);
  8.   g = &((LG *)L)->g;
  9.   L->next = NULL;
  10.   L->tt = LUA_TTHREAD;
  11.   g->currentwhite = bit2mask(WHITE0BITFIXEDBIT);
  12.   L->marked = luaC_white(g);
  13.   set2bits(L->marked, FIXEDBITSFIXEDBIT);
  14.   preinit_state(Lg);
  15.   g->frealloc = f;
  16.   g->ud = ud;
  17.   g->mainthread = L;
  18.   g->uvhead.u.l.prev = &g->uvhead;
  19.   g->uvhead.u.l.next = &g->uvhead;
  20.   g->GCthreshold = 0;  /* mark it as unfinished state */
  21.   g->strt.size = 0;
  22.   g->strt.nuse = 0;
  23.   g->strt.hash = NULL;
  24.   setnilvalue(registry(L));
  25.   luaZ_initbuffer(L, &g->buff);
  26.   g->panic = NULL;
  27.   g->gcstate = GCSpause;
  28.   g->rootgc = obj2gco(L);
  29.   g->sweepstrgc = 0;
  30.   g->sweepgc = &g->rootgc;
  31.   g->gray = NULL;
  32.   g->grayagain = NULL;
  33.   g->weak = NULL;
  34.   g->tmudata = NULL;
  35.   g->totalbytes = sizeof(LG);
  36.   g->gcpause = LUAI_GCPAUSE;
  37.   g->gcstepmul = LUAI_GCMUL;
  38.   g->gcdept = 0;
  39.   InitializeCriticalSection(&g->m_cs);
  40.   for (i=0; i<NUM_TAGSi++) g->mt[i] = NULL;
  41.   if (luaD_rawrunprotected(Lf_luaopenNULL) != 0) {
  42.     /* memory allocation error: free partial state */
  43.     close_state(L);
  44.     L = NULL;
  45.   }
  46.   else
  47.     luai_userstateopen(L);
  48.   return L;
  49. }

3.在close_state 加入删除CriticalSection的代码 DeleteCriticalSection(&g->m_cs);

Code Snippet
  1. static void close_state (lua_State *L) {
  2.   global_State *g = G(L);
  3.   luaF_close(LL->stack);  /* close all upvalues for this thread */
  4.   luaC_freeall(L);  /* collect all objects */
  5.   lua_assert(g->rootgc == obj2gco(L));
  6.   lua_assert(g->strt.nuse == 0);
  7.   luaM_freearray(LG(L)->strt.hashG(L)->strt.sizeTString *);
  8.   luaZ_freebuffer(L, &g->buff);
  9.   freestack(LL);
  10.   lua_assert(g->totalbytes == sizeof(LG));
  11.   DeleteCriticalSection(&g->m_cs);
  12.   (*g->frealloc)(g->udfromstate(L), state_size(LG), 0);
  13. }

4.修改lua_lock和lua_unlock宏如下

Code Snippet
  1. #ifndef lua_lock
  2. #define lua_lock(LEnterCriticalSection(&(G(L)->m_cs))
  3. #define lua_unlock(LLeaveCriticalSection(&(G(L)->m_cs))
  4. #endif

5.为了使用CRITICAL_SECTION我们比较通用的方法是包含头文件 windows.h,但包含后发现,lua库中的LoadString函数正好与windows  api LoadString同名。所以把lua库内部所使用的LoadString通通改为LUA_LoadString即可

修改完之后,在控制台写了四个线程一个一个主lua_state,四个线程使用lua_newthread得到的lua_state,没有再出现错误了。

相关文章
|
14天前
|
存储 Java 数据库连接
java多线程之线程通信
java多线程之线程通信
|
25天前
|
存储 缓存 NoSQL
Redis单线程已经很快了6.0引入多线程
Redis单线程已经很快了6.0引入多线程
31 3
|
27天前
|
消息中间件 安全 Linux
线程同步与IPC:单进程多线程环境下的选择与权衡
线程同步与IPC:单进程多线程环境下的选择与权衡
57 0
|
1月前
|
Java 调度 C#
C#学习系列相关之多线程(一)----常用多线程方法总结
C#学习系列相关之多线程(一)----常用多线程方法总结
|
1月前
|
安全 编译器 C#
C#学习相关系列之多线程---lock线程锁的用法
C#学习相关系列之多线程---lock线程锁的用法
|
1月前
|
Java C#
C#学习系列相关之多线程(五)----线程池ThreadPool用法
C#学习系列相关之多线程(五)----线程池ThreadPool用法
|
1月前
|
存储 安全 Java
深入理解 Java 多线程、Lambda 表达式及线程安全最佳实践
线程使程序能够通过同时执行多个任务而更有效地运行。 线程可用于在不中断主程序的情况下在后台执行复杂的任务。 创建线程 有两种创建线程的方式。 扩展Thread类 可以通过扩展Thread类并覆盖其run()方法来创建线程:
107 1
深入理解 Java 多线程、Lambda 表达式及线程安全最佳实践
|
1月前
|
缓存 安全 Java
保障线程安全性:构建可靠的多线程应用
保障线程安全性:构建可靠的多线程应用
|
2月前
|
Java
网络 I/O:单 Selector 多线程(单线程模型)
网络 I/O:单 Selector 多线程(单线程模型)
|
1月前
|
数据采集 存储 Java
「多线程大杀器」Python并发编程利器:ThreadPoolExecutor,让你一次性轻松开启多个线程,秒杀大量任务!
「多线程大杀器」Python并发编程利器:ThreadPoolExecutor,让你一次性轻松开启多个线程,秒杀大量任务!

热门文章

最新文章