歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

Lua 架構構建多線程事物性線程

Lua與C++的調用本文雖然有涉及但是,這不是我今天向大家推薦的內容,今天向大家寫的示例代碼是Lua如何構建控制和任務邏輯以及與主線程進行控制子線程程執行的方式。

不熟悉Windows下多線程的童鞋可以翻翻本人的多線程的相關文章,也可以自己從網上學習下多線程的編寫,以及如何合理的退出線程。這樣編寫代碼可以做到,架構靈活,業務可變更性非常強,只要實現好底層算法性代碼,業務邏輯盡量交給腳本語言吧,查錯方便,直接可以定位到具體的行數,這是C++不具備的,當然上層用Lua方便的同時,增加了編寫C++注冊到Lua的函數的人負擔,他必須非常清楚的了解Lua和C++的特性,但是對於分別編寫Lua和C++的兩撥人將非常輕松,這樣團隊只需要一個C++能人和一些中級的C++程序員,和一堆對編程了解不多的Lua程序員就可以完成復雜的業務邏輯工作。最適合團隊中只要一個技術大神的團隊或者團隊中的大神非常少的情況。這樣設計從根本上屏蔽了對C++多線程的了解,Lua程序員就可以處理多線程並發事物,他可以不知道何為原子操作,可以不知道何為關鍵段何為等待事物,這些全部交給一個去編寫完成,其他Lua程序員只需要調用即可,而不是神級的C++程序員只用來開發底層費時間的運算即可。非常適合快速開發。若後期覺得提高效率非常必要,那就將Lua寫成C++。

  1. #include <stdio.h> 
  2. #include <iostream> 
  3. #include <Windows.h> 
  4. #include <vector> 
  5.  
  6.  
  7. #pragma comment(lib,"lua.lib") 
  8. using namespace std; 
  9.  
  10.  
  11. extern "C" { 
  12. #include "lua/lua.h" 
  13. #include "lua/lualib.h" 
  14. #include "lua/lauxlib.h" 
  15. } 
  16.  
  17.  
  18. lua_State* L; 
  19. lua_State* L2; 
  20.  
  21.  
  22. int average(lua_State *L) 
  23. { 
  24.     int n=lua_gettop(L); 
  25.     double sum=0; 
  26.     for (int i=0;i<=n;i++) 
  27.     { 
  28.         sum+=lua_tonumber(L,i); 
  29.     } 
  30.     lua_pushnumber(L,sum/n); 
  31.     lua_pushnumber(L,sum); 
  32.     lua_pushnumber(L,n); 
  33.     /*標識函數有幾個返回值*/ 
  34.     return 3; 
  35. } 
  36. HANDLE MyEvent; 
  37. HANDLE MyExit; 
  38. volatile unsigned long threadRun; 
  39.  
  40.  
  41. struct ThreadInfo 
  42. { 
  43.     string Text; 
  44.      
  45.     string DataPath; 
  46.  
  47.  
  48.     int    NextPosition; 
  49.  
  50.  
  51.     bool   boolValue; 
  52. }; 
  53.  
  54.  
  55. vector <int(*)(ThreadInfo InputThreadInfo,ThreadInfo &OutputThreadInfo)> FunctionVector; 
  56.  
  57.  
  58. int AddFunction(int(*_ProcesseFunction)(ThreadInfo InputThreadInfo,ThreadInfo &OutputThreadInfo)) 
  59. { 
  60.     FunctionVector.push_back(_ProcesseFunction); 
  61.     return 0; 
  62. } 
  63.  
  64.  
  65. ThreadInfo myInputInfo; 
  66.  
  67.  
  68. static unsigned long WINAPI MyThreadFunction(LPVOID lpvoid) 
  69. { 
  70.     myInputInfo.boolValue    = false; 
  71.     myInputInfo.DataPath     = "kji"; 
  72.     myInputInfo.NextPosition = 0; 
  73.     myInputInfo.Text         = "Hello World!"; 
  74.     ThreadInfo myOutPutInfo; 
  75.      
  76.     while(1) 
  77.     { 
  78.         WaitForSingleObject(MyEvent,INFINITE); 
  79.         ResetEvent(MyEvent); 
  80.         if(0==threadRun) 
  81.         { 
  82.             break; 
  83.         } 
  84.         for (int i=0;i<(int)(FunctionVector.size());i++) 
  85.         { 
  86.             if (NULL!=(FunctionVector[i])) 
  87.             { 
  88.                 (*FunctionVector[i])(myInputInfo,myOutPutInfo); 
  89.             } 
  90.         }         
  91.     } 
  92.     SetEvent(MyExit); 
  93.     return 0; 
  94. } 
  95.  
  96.  
  97. int PointFunction(ThreadInfo InputThreadInfo,ThreadInfo &OutputThreadInfo) 
  98. { 
  99.     OutputThreadInfo.boolValue=!InputThreadInfo.boolValue; 
  100.     OutputThreadInfo.DataPath= InputThreadInfo.DataPath; 
  101.     OutputThreadInfo.NextPosition = InputThreadInfo.NextPosition+1; 
  102.     OutputThreadInfo.Text = InputThreadInfo.Text; 
  103.  
  104.  
  105.     printf("%s \n",OutputThreadInfo.Text); 
  106.     /* 運行腳本 */ 
  107.     luaL_dofile(L2, "2.lua"); 
  108. } 
  109.  
  110.  
  111. int startWork(lua_State *L) 
  112. { 
  113.     SetEvent(MyEvent); 
  114.     return 0; 
  115. } 
  116.  
  117.  
  118. int Exit(lua_State *L) 
  119. { 
  120.     InterlockedExchange(&threadRun,0); 
  121.     SetEvent(MyEvent); 
  122.     return 0; 
  123. } 
  124.  
  125.  
  126. int CreateMythread(LPVOID lpvoid) 
  127. { 
  128.     MyEvent = CreateEvent(NULL,TRUE,FALSE,L"startProcess"); 
  129.     MyExit  = CreateEvent(NULL,TRUE,FALSE,L"ExitEvent"); 
  130.     InterlockedExchange(&threadRun,1); 
  131.     DWORD dwthreadID; 
  132.     HANDLE ThreadHandle = CreateThread(NULL,NULL,MyThreadFunction,lpvoid,0,&dwthreadID); 
  133.     if (NULL==ThreadHandle) 
  134.     { 
  135.         CloseHandle(ThreadHandle); 
  136.         return 1; 
  137.     } 
  138.     CloseHandle(ThreadHandle); 
  139.     return 0; 
  140. } 
  141.  
  142.  
  143.  
  144.  
  145. int main ( int argc, char *argv[] ) 
  146. { 
  147.     AddFunction(PointFunction); 
  148.     CreateMythread(NULL); 
  149.  
  150.  
  151.     /* 初始化Lua */ 
  152.     L=luaL_newstate(); 
  153.     lua_status(L); 
  154.  
  155.  
  156.     /* 載入Lua基本庫 */ 
  157.     luaL_openlibs(L); 
  158.  
  159.  
  160.     /*注冊函數*/ 
  161.      
  162.     L2=luaL_newstate(); 
  163.     lua_status(L2); 
  164.  
  165.  
  166.     /* 載入Lua基本庫 */ 
  167.     luaL_openlibs(L2); 
  168.  
  169.  
  170.     /*注冊函數*/ 
  171.  
  172.  
  173.     lua_register(L2,"average",average); 
  174.  
  175.  
  176.     lua_register(L,"startWork",startWork); 
  177.  
  178.  
  179.     lua_register(L,"ExitAllThread",Exit); 
  180.     /* 運行腳本 */ 
  181.     luaL_dofile(L, "1.lua"); 
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  
  188.     WaitForSingleObject(MyExit,INFINITE); 
  189.     ResetEvent(MyExit); 
  190.     CloseHandle(MyExit); 
  191.     CloseHandle(MyEvent); 
  192.     /* 清除Lua */ 
  193.     lua_close(L); 
  194.     lua_close(L2); 
  195.     /* 暫停 */ 
  196.     system("pause"); 
  197.  
  198.  
  199.     return 0; 
  200. } 
Copyright © Linux教程網 All Rights Reserved