Home > other >  Lua multithreaded collapse
Lua multithreaded collapse

Time:09-23


C + + multithreaded call lua method, will collapse from time to time,
Modified the lua code lua_lock and lua_unlock macros, locking situations also will collapse (see http://www.cnblogs.com/zhangdongsheng/p/3679024.html)
Here is my code,


===========================================test. The lua test code is as follows:

The local t=Thread: getInstance ();//here is the c + + mapping to the lua way
For I=1, 10, 1 do
//start threads to execute the following c + + lua methods
T: start (function ()
//here is the new thread handle
For j=1, 100000, 1 do
G: log (I.. "# # # # j:".. The tostring (j));
End
End);
End


===========================================lua_thread. CPP code is as follows: (the following code is registered Thread. The function of h for lua)
Lua can
The local t=Thread: getInstance (); Get a Thread object, can also be local t=Thread: new ();
T: start (function ()
//here is the new thread handle
End);
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

/*
* * the Lua binding: ThreadToLua
*/

# include "lua_thread. HPP
"
# # ifndef __cplusplus
# include "stdlib. H"
# endif
# include "string. H"
# include "tolua_fix. H"
# include "Thread. H"

# include "CCLuaValue. H"
USING_NS_CC;

/* Exported function */
//TOLUA_API int register_lua_thread (lua_State * tolua_S);

/* function to register the type */
The static void tolua_reg_types (lua_State * tolua_S)
{
Tolua_usertype (tolua_S, "Thread");
}

/* method: getInstance of class Thread */
# # ifndef TOLUA_DISABLE_tolua_thread_getInstance
Static int tolua_thread_getInstance (lua_State * tolua_S)
{
# # ifndef TOLUA_RELEASE
Tolua_Error tolua_err;
If (
! Tolua_isusertable (tolua_S, 1, "Thread", 0, & amp; Tolua_err) | |
! Tolua_isnoobj (tolua_S, 2, & amp; Tolua_err)
)
Goto tolua_lerror;
The else
# endif
{
{
Thread * tolua_ret=(Thread *) Thread: : getInstance ();
Tolua_ret tolua_pushusertype (tolua_S, (void *), "Thread");
}
}
return 1;
# # ifndef TOLUA_RELEASE
Tolua_lerror:
Tolua_error (tolua_S, "# ferror function in 'getInstance'.", & amp; Tolua_err);
return 0;
# endif
}
# endif//# # ifndef TOLUA_DISABLE}

/* method: newInstance of class Thread */
# # ifndef TOLUA_DISABLE_tolua_thread_newInstance
Static int tolua_thread_newInstance (lua_State * tolua_S)
{
# # ifndef TOLUA_RELEASE
Tolua_Error tolua_err;
If (
! Tolua_isusertable (tolua_S, 1, "Thread", 0, & amp; Tolua_err) | |
! Tolua_isnoobj (tolua_S, 2, & amp; Tolua_err)
)
Goto tolua_lerror;
The else
# endif
{
{
Thread * tolua_ret=(Thread *) Thread: : newInstance ();
Tolua_ret tolua_pushusertype (tolua_S, (void *), "Thread");
}
}
return 1;
# # ifndef TOLUA_RELEASE
Tolua_lerror:
Tolua_error (tolua_S, "# ferror function in 'newInstance'.", & amp; Tolua_err);
return 0;
# endif
}
# endif//# # ifndef TOLUA_DISABLE

/* method: the start of class Thread */
# # ifndef TOLUA_DISABLE_tolua_thread_start
Static int tolua_thread_start (lua_State * tolua_S)
{
# # ifndef TOLUA_RELEASE
Tolua_Error tolua_err;
If (
! Tolua_isusertype (tolua_S, 1, "Thread", 0, & amp; Tolua_err) | |
//! Tolua_isnumber (tolua_S, 2, 0, & amp; Tolua_err) | |
! Toluafix_isfunction (tolua_S, 2, "LUA_FUNCTION," 0, & amp; Tolua_err) | |
! Tolua_isnoobj (tolua_S, 3, & amp; Tolua_err)
)
Goto tolua_lerror;
The else
# endif
{
Thread * self=(Thread *) tolua_tousertype (tolua_S, 1, 0);
//int luaCallback=((int) tolua_tonumber (tolua_S, 2, 0));
LUA_FUNCTION luaCallback=(toluafix_ref_function (tolua_S, 2, 0));
# # ifndef TOLUA_RELEASE
if (! The self) tolua_error (tolua_S, "invalid 'self' function in 'start'", NULL);
# endif
{
The self - & gt; Start (luaCallback);
}
}
return 0;
# # ifndef TOLUA_RELEASE
Tolua_lerror:
Tolua_error (tolua_S, "# ferror function in 'start'.", & amp; Tolua_err);
return 0;
# endif
}
# endif//# # ifndef TOLUA_DISABLE

/* the Open function */
TOLUA_API int register_lua_thread (lua_State * tolua_S)
{
Tolua_open (tolua_S);
Tolua_reg_types (tolua_S);
Tolua_module (tolua_S, NULL, 0);
Tolua_beginmodule (tolua_S, NULL);
Tolua_cclass (tolua_S, "Thread", "Thread", "", NULL);
Tolua_beginmodule (tolua_S, "Thread");
Tolua_function (tolua_S, getInstance, tolua_thread_getInstance);
Tolua_function (tolua_S, "new", tolua_thread_newInstance);
Tolua_function (tolua_S, "start", tolua_thread_start);
Tolua_endmodule (tolua_S);
Tolua_endmodule (tolua_S);
return 1;
}

# if defined (LUA_VERSION_NUM) & amp; & LUA_VERSION_NUM & gt;=501
TOLUA_API int luaopen_thread (lua_State * tolua_S) {
Return register_lua_thread (tolua_S);
};
# endif


===========================================Thread. The h code is as follows:

# # ifndef __LUA_Thread_H__
# define __LUA_Thread_H__

# include "cocos2d. H"
# include "CCLuaStack. H"
# include "ThreadSafeQueue. H"
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related