zoukankan      html  css  js  c++  java
  • 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 f, void *ud) {
    2.   int i;
    3.   lua_State *L;
    4.   global_State *g;
    5.   void *l = (*f)(ud, NULL, 0, state_size(LG));
    6.   if (l == NULL) return NULL;
    7.   L = tostate(l);
    8.   g = &((LG *)L)->g;
    9.   L->next = NULL;
    10.   L->tt = LUA_TTHREAD;
    11.   g->currentwhite = bit2mask(WHITE0BIT, FIXEDBIT);
    12.   L->marked = luaC_white(g);
    13.   set2bits(L->marked, FIXEDBIT, SFIXEDBIT);
    14.   preinit_state(L, g);
    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_TAGS; i++) g->mt[i] = NULL;
    41.   if (luaD_rawrunprotected(L, f_luaopen, NULL) != 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(L, L->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(L, G(L)->strt.hash, G(L)->strt.size, TString *);
    8.   luaZ_freebuffer(L, &g->buff);
    9.   freestack(L, L);
    10.   lua_assert(g->totalbytes == sizeof(LG));
    11.   DeleteCriticalSection(&g->m_cs);
    12.   (*g->frealloc)(g->ud, fromstate(L), state_size(LG), 0);
    13. }

    4.修改lua_lock和lua_unlock宏如下

    Code Snippet
    1. #ifndef lua_lock
    2. #define lua_lock(L) EnterCriticalSection(&(G(L)->m_cs))
    3. #define lua_unlock(L) LeaveCriticalSection(&(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,没有再出现错误了。

  • 相关阅读:
    实验11——指针的基础应用
    C语言程序设计第10堂作业
    实验九——基本数据类型存储及应用总结
    C语言程序设计第8堂作业
    实验七——函数定义及调用总结
    实验六——循环结构程序练习总结
    实验五——循环结构学习总结
    实验四——多分支结构及本章总结
    9.29第三次作业
    zuoyeQAQ
  • 原文地址:https://www.cnblogs.com/zhangdongsheng/p/3679024.html
Copyright © 2011-2022 走看看