Use the Lua method in multiple threads

Source: Internet
Author: User

InMultithreadingUsed inLuaThe method is the content to be introduced in this article, as you know, until now lua 5.1,LuaThe functions in are not provided with thread-safe implementation. So ifMultithreadingWhen you access lua_State in, unexpected results are generated. However, the current application software generally requiresMultithreadingTo meet the application requirements.

If you cannotMultithreadingUsed inLuaThis will be a huge limit for Lua. But is there any way to solve this problem? Fortunately, lua provides multi-threaded functions to solve the problem of multithreading. The five function prototypes are as follows:

 
 
  1. int lua_newthred(lua_State* L)  
  2. int lua_resume(lua_State* L, int nargs)  
  3. int lua_yield(lua_State* L int nresults)  
  4. void lua_lock(lua_State* L)  
  5. void lua_unlock(lua_State* L) 

We use the above five functions, coupled with thread synchronization. Basically, it can solve the application problem of Lua in multithreading. For detailed usage of the above five functions, see the lua Development Guide. In the following example, the corresponding code is provided for your reference.

1. a Lua stack error occurs when multiple threads are used,

Cause of the problem: using the same lua_State in multiple threads can lead to errors in the lua stack.

Solution: Use the lua_newthread function to generate a new stack to prevent stack access conflicts.

Solution code example: in actual use, you need to maintain lua stack access conflicts with caution. When you call lua_newthread, you also need to synchronize the mechanism to protect it. You need to implement lua_lock and lua_unlock, to ensure that multi-threaded access does not conflict. ):

 
 
  1.  lua_State* L = lua_newthread(luaMain)  
  2.   ...  
  3.  lua_pushstring(L, str);  
  4. ...  
  5. lua_resume(L, n) 

2. Pause the execution of the Lua script, but prevent the main thread of the Host Program from getting stuck.

Solution: You can call the lua_yield function in the Host Program to pause script execution. When the host program completes the execution task, call lua_resume to resume the execution of the lua script.

The sample code is as follows:

 
 
  1. C ++:
  2. Int show_dialog (void)
  3. {
  4. ....
  5. Lua_pushnumber (L, IDOK );
  6. Return lua_yield (L, n); The \ lua_yield function must be placed behind the return statement.
  7. }
  8. Int end_dialog (void)
  9. {
  10. ...
  11. Int ret = lua_resume (L, 0 );
  12. If (ret = LUA_YIELD)
  13. {
  14. Int id = lua_tonumber (L,-1 );
  15. }
  16. Return 2;
  17. }
  18. Lua:
  19. Ret = show_dialog ()
  20. If ret = 0 then
  21. End

Summary:MultithreadingUsed inLuaThe content of this method has been introduced. I hope this article will help you!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.