Lua 架构构建多线程事物性线程
Lua与C++的调用本文虽然有涉及但是,这不是我今天向大家推荐的内容,今天向大家写的示例代码是Lua如何构建控制和任务逻辑以及与主线程进行控制子线程程执行的方式。
不熟悉Windows下多线程的童鞋可以翻翻本人的多线程的相关文章,也可以自己从网上学习下多线程的编写,以及如何合理的退出线程。这样编写代码可以做到,架构灵活,业务可变更性非常强,只要实现好底层算法性代码,业务逻辑尽量交给脚本语言吧,查错方便,直接可以定位到具体的行数,这是C++不具备的,当然上层用Lua方便的同时,增加了编写C++注册到Lua的函数的人负担,他必须非常清楚的了解Lua和C++的特性,但是对于分别编写Lua和C++的两拨人将非常轻松,这样团队只需要一个C++能人和一些中级的C++程序员,和一堆对编程了解不多的Lua程序员就可以完成复杂的业务逻辑工作。最适合团队中只要一个技术大神的团队或者团队中的大神非常少的情况。这样设计从根本上屏蔽了对C++多线程的了解,Lua程序员就可以处理多线程并发事物,他可以不知道何为原子操作,可以不知道何为关键段何为等待事物,这些全部交给一个去编写完成,其他Lua程序员只需要调用即可,而不是神级的C++程序员只用来开发底层费时间的运算即可。非常适合快速开发。若后期觉得提高效率非常必要,那就将Lua写成C++。
- #include <stdio.h>
- #include <iostream>
- #include <Windows.h>
- #include <vector>
- #pragma comment(lib,"lua.lib")
- using namespace std;
- extern "C" {
- #include "lua/lua.h"
- #include "lua/lualib.h"
- #include "lua/lauxlib.h"
- }
- lua_State* L;
- lua_State* L2;
- int average(lua_State *L)
- {
- int n=lua_gettop(L);
- double sum=0;
- for (int i=0;i<=n;i++)
- {
- sum+=lua_tonumber(L,i);
- }
- lua_pushnumber(L,sum/n);
- lua_pushnumber(L,sum);
- lua_pushnumber(L,n);
- /*标识函数有几个返回值*/
- return 3;
- }
- HANDLE MyEvent;
- HANDLE MyExit;
- volatile unsigned long threadRun;
- struct ThreadInfo
- {
- string Text;
- string DataPath;
- int NextPosition;
- bool boolValue;
- };
- vector <int(*)(ThreadInfo InputThreadInfo,ThreadInfo &OutputThreadInfo)> FunctionVector;
- int AddFunction(int(*_ProcesseFunction)(ThreadInfo InputThreadInfo,ThreadInfo &OutputThreadInfo))
- {
- FunctionVector.push_back(_ProcesseFunction);
- return 0;
- }
- ThreadInfo myInputInfo;
- static unsigned long WINAPI MyThreadFunction(LPVOID lpvoid)
- {
- myInputInfo.boolValue = false;
- myInputInfo.DataPath = "kji";
- myInputInfo.NextPosition = 0;
- myInputInfo.Text = "Hello World!";
- ThreadInfo myOutPutInfo;
- while(1)
- {
- WaitForSingleObject(MyEvent,INFINITE);
- ResetEvent(MyEvent);
- if(0==threadRun)
- {
- break;
- }
- for (int i=0;i<(int)(FunctionVector.size());i++)
- {
- if (NULL!=(FunctionVector[i]))
- {
- (*FunctionVector[i])(myInputInfo,myOutPutInfo);
- }
- }
- }
- SetEvent(MyExit);
- return 0;
- }
- int PointFunction(ThreadInfo InputThreadInfo,ThreadInfo &OutputThreadInfo)
- {
- OutputThreadInfo.boolValue=!InputThreadInfo.boolValue;
- OutputThreadInfo.DataPath= InputThreadInfo.DataPath;
- OutputThreadInfo.NextPosition = InputThreadInfo.NextPosition+1;
- OutputThreadInfo.Text = InputThreadInfo.Text;
- printf("%s \n",OutputThreadInfo.Text);
- /* 运行脚本 */
- luaL_dofile(L2, "2.lua");
- }
- int startWork(lua_State *L)
- {
- SetEvent(MyEvent);
- return 0;
- }
- int Exit(lua_State *L)
- {
- InterlockedExchange(&threadRun,0);
- SetEvent(MyEvent);
- return 0;
- }
- int CreateMythread(LPVOID lpvoid)
- {
- MyEvent = CreateEvent(NULL,TRUE,FALSE,L"startProcess");
- MyExit = CreateEvent(NULL,TRUE,FALSE,L"ExitEvent");
- InterlockedExchange(&threadRun,1);
- DWORD dwthreadID;
- HANDLE ThreadHandle = CreateThread(NULL,NULL,MyThreadFunction,lpvoid,0,&dwthreadID);
- if (NULL==ThreadHandle)
- {
- CloseHandle(ThreadHandle);
- return 1;
- }
- CloseHandle(ThreadHandle);
- return 0;
- }
- int main ( int argc, char *argv[] )
- {
- AddFunction(PointFunction);
- CreateMythread(NULL);
- /* 初始化Lua */
- L=luaL_newstate();
- lua_status(L);
- /* 载入Lua基本库 */
- luaL_openlibs(L);
- /*注册函数*/
- L2=luaL_newstate();
- lua_status(L2);
- /* 载入Lua基本库 */
- luaL_openlibs(L2);
- /*注册函数*/
- lua_register(L2,"average",average);
- lua_register(L,"startWork",startWork);
- lua_register(L,"ExitAllThread",Exit);
- /* 运行脚本 */
- luaL_dofile(L, "1.lua");
- WaitForSingleObject(MyExit,INFINITE);
- ResetEvent(MyExit);
- CloseHandle(MyExit);
- CloseHandle(MyEvent);
- /* 清除Lua */
- lua_close(L);
- lua_close(L2);
- /* 暂停 */
- system("pause");
- return 0;
- }
相关推荐
瓜牛呱呱 2020-11-12
starinshy 2020-11-10
farewellpoem 2020-11-09
lhtzbj 2020-08-13
cuiweisaidelike 2020-08-02
comeonxueRong 2020-08-02
learnpy 2020-07-19
kyelu 2020-07-09
yunfeitian 2020-07-05
zhoujiyu 2020-06-28
TreasureZ 2020-06-25
三动 2020-06-21
huichen 2020-10-13
WenSenli 2020-10-12
jvm 2020-06-14
举 2020-06-14
这些年来 2020-06-12