工作原理

由于lua只能单线程运行,该lib要求所有lua代码在单线程,而多线程部分只能为c代码

具体用法上要求多线程部分必须用c实现

相关模块

线程池

异步函数实现框架

Now only a sleep function is provided

Usage:

function test2_threadpool()
local tp = Dll.MyTHdPool()
local n =
local function f()
n = n+
print('f ' .. n)
if(n==) then return end
tp:sleep1(, f)
end f()
tp:join()
end

C codes:

#include "stdafx.h"
#include <luabind.hpp>
#include <vector>
#include <queue>
#include <boost/thread.hpp> using namespace luabind; #include "stdafx.h" #include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/thread/thread.hpp> #include <deque> class ThreadPool
{
boost::asio::io_service ioService;
boost::thread_group threadpool;
boost::asio::io_service::work work;
public:
ThreadPool() :work(ioService)
{
/*
* This will start the ioService processing loop. All tasks
* assigned with ioService.post() will start executing.
*/
//boost::asio::io_service::work work(ioService); /*
* This will add threads to the thread pool. (You could just put it in a for loop)
*/
threadpool.create_thread(
boost::bind(&boost::asio::io_service::run, &ioService)
);
threadpool.create_thread(
boost::bind(&boost::asio::io_service::run, &ioService)
); }
~ThreadPool()
{ } void post(boost::function<void()> f)
{
ioService.post(f);
} void join()
{
threadpool.join_all();
}
private: }; namespace bamthread
{
typedef std::unique_ptr<boost::asio::io_service::work> asio_worker; struct ThreadPool {
ThreadPool(size_t threads) :service(), working(new asio_worker::element_type(service)) {
while (threads--)
{
auto worker = boost::bind(&boost::asio::io_service::run, &(this->service));
g.add_thread(new boost::thread(worker));
}
} template<class F>
void post(F f){
service.post(f);
} ~ThreadPool() {
working.reset(); //allow run() to exit
g.join_all();
service.stop();
} private:
boost::asio::io_service service; //< the io_service we are wrapping
asio_worker working;
boost::thread_group g; //< need to keep track of threads so we can join them
};
} void my_task()
{
Sleep();
printf("mytask");
} void test1()
{
bamthread::ThreadPool tp();
tp.post(boost::bind(my_task));
//tp.join();
} void test()
{
/*
* Create an asio::io_service and a thread_group (through pool in essence)
*/
boost::asio::io_service ioService;
boost::thread_group threadpool; /*
* This will start the ioService processing loop. All tasks
* assigned with ioService.post() will start executing.
*/
boost::asio::io_service::work work(ioService); /*
* This will add threads to the thread pool. (You could just put it in a for loop)
*/
threadpool.create_thread(
boost::bind(&boost::asio::io_service::run, &ioService)
);
threadpool.create_thread(
boost::bind(&boost::asio::io_service::run, &ioService)
); /*
* This will assign tasks to the thread pool.
* More about boost::bind: "http://www.boost.org/doc/libs/1_54_0/libs/bind/bind.html#with_functions"
*/
ioService.post(boost::bind(my_task)); /*
* This will stop the ioService processing loop. Any tasks
* you add behind this point will not execute.
*/
ioService.stop(); /*
* Will wait till all the threads in the thread pool are finished with
* their assigned tasks and 'join' them. Just assume the threads inside
* the threadpool will be destroyed by this method.
*/
threadpool.join_all();
} template <typename T>
class queue
{
private:
boost::mutex d_mutex;
boost::condition_variable d_condition;
std::deque<T> d_queue;
public:
void push(T const& value) {
{
boost::unique_lock<boost::mutex> lock(this->d_mutex);
d_queue.push_front(value);
}
this->d_condition.notify_one();
}
T pop() {
boost::unique_lock<boost::mutex> lock(this->d_mutex);
this->d_condition.wait(lock, [=]{ return !this->d_queue.empty(); });
T rc(std::move(this->d_queue.back()));
this->d_queue.pop_back();
return rc;
}
}; class MyTHdPool
{
bamthread::ThreadPool tp; boost::mutex m;
std::map<int, boost::function<void()> > f2s; // key: taskid, value: post processing //boost::thread t_; queue<int> q_;
int taskid_; public:
MyTHdPool() :tp(), taskid_(){} ~MyTHdPool(){
join();
} void Call(boost::function<void()> f1, boost::function<void()> f2)
{
int taskid = taskid_++; printf("begin call task %d\n", taskid); boost::function<void()> f = [=]() mutable {
f1(); q_.push(taskid);
printf("done task %d\n", taskid);
}; {
boost::lock_guard<boost::mutex> lock(m);
f2s[taskid] = (f2);
} tp.post(f);
printf("end post task %d\n", taskid);
} void join()
{
while (true)
{
boost::function<void()> f2;
int taskid = ;
{
{
boost::lock_guard<boost::mutex> lock(m);
if (f2s.empty())
return;
} printf("start pop a task from queue\n");
int taskid = q_.pop();
printf("got a task %d from queue\n", taskid); {
boost::lock_guard<boost::mutex> lock(m);
auto it = f2s.find(taskid);
assert(it != f2s.end());
f2 = it->second;
f2s.erase(it);
}
} printf("exec task post ftn %d\n", taskid);
f2();
}
} void sleep1(double n, object f2)
{
Call([n](){Sleep(n * ); }, [f2, this]() mutable {
f2();
});
} void sleep2(double n)
{
Call([n](){Sleep(n * ); }, [](){});
}
private: }; void callback(object o)
{
printf("before callback\n");
o();
printf("after callback\n");
} int luaopen_Dll(lua_State* L)
{ luaL_openlibs(L);
open(L); // define a module in _G["t"]
module(L, "Dll")[ class_<MyTHdPool>("MyTHdPool")
.def(constructor<>())
.def("sleep1", &MyTHdPool::sleep1)
.def("sleep2", &MyTHdPool::sleep2)
.def("join", &MyTHdPool::join), def("test1", &test1), def("callback", &callback)
]; // push _G["t"] to stack
lua_getglobal(L, "Dll"); // set _G["t"]=nil
lua_pushnil(L);
lua_setglobal(L, "Dll"); return ;
}

Written a lua threadpool的更多相关文章

  1. [转]LUA元表

    lua元表和元方法 <lua程序设计> 13章 读书笔记 lua中每个值都有一个元表,talble和userdata可以有各自独立的元表,而其它类型的值则共享其类型所属的单一元表.lua在 ...

  2. Lua 笔记

    lua命令: #enter shell lua #excute script file lua xxx.lua lua脚本: #!/usr/local/bin/lua 核心概念: As a exten ...

  3. lua function

    This is the main reference for the World of Warcraft Lua Runtime. Note that these are mostly standar ...

  4. Lua参考手册

    英文原版: http://www.lua.org/manual/5.1/ 中文版下面2个地址都有:一样的 manual.luaer.cn lua在线手册 lua参考手册Lua参考手册的中文翻译(云风翻 ...

  5. lua: Learning Official Doc notes

    dynamically typed vars: basic types: nil, boolean, number, string, function, userdata, thread & ...

  6. Lua学习系列(五)

    calling C functions from Lua 5.2 这篇文章也不错: http://blog.csdn.net/x356982611/article/details/26688287 h ...

  7. Lua的各种资源1

    Libraries And Bindings     LuaDirectory > LuaAddons > LibrariesAndBindings This is a list of l ...

  8. luaprofiler探索

    什么是luaprofiler? http://luaprofiler.luaforge.net/manual.html LuaProfiler is a time profiler designed ...

  9. Popular Deep Learning Tools – a review

    Popular Deep Learning Tools – a review Deep Learning is the hottest trend now in AI and Machine Lear ...

随机推荐

  1. 用jQuery实现参数自定义的文字跑马灯效果

    一,明确需求 基本需求:最近在工作中接到一个新需求,简单来说就是实现一行文字从右到左跑马灯的效果,并且以固定的时间间隔进行循环. 原本这是一个很容易实现的需求,但是难点是要求很多参数得是用户可自行设置 ...

  2. vscode 常用插件

    1. Local History 可以查看本地的修改记录,比较不同 2.TODO插件 可以将有待修改或者完善的地方标记出来,在任务列表中查看, 避免后面忘记, 提高效率. 包含TODO, FIXME. ...

  3. 一起用ipython

    安装 安装python2版本的软件包就用命令 pip install ipython 安装python3版本对应的软件包就用命令 pip3 install ipython 进入了ipython,ipy ...

  4. js在线富文本插件的考虑

    使用之前需要考虑的点: 1需要插件,是否需要很多的插件,还是说简单的那些功能就行了. 2界面考虑,看你喜欢那个界面了. 3图片是否需要上传图片服务器. 4文档如果为英文是否会影响开发. 5支持浏览器类 ...

  5. Ajax+Struts2用户注册功能实现

    详细请参考源码(Github):https://github.com/QQ3330447288/ajaxRegister 1.目录结构 2.截图 3.核心代码: register.jsp <sc ...

  6. gii的使用

    假设有表link 在backend中生成子模块content 生成子模块模型common/models/Link 生成模块content中的crud 配置别名 在backend/config/main ...

  7. 图论++【洛谷p1744】特价采购商品&&【一本通1342】最短路径问题

    (虽然题面不是很一样,但是其实是一个题qwq) [传送门] 算法标签: 利用Floyed的o(n3)算法: (讲白了就是暴算qwq) 从任意一条单边路径开始.所有两点之间的距离是边的权,或者无穷大,如 ...

  8. JavaScript基础三

    1.10 关于DOM的事件操作 1.10.1 JavaScript的组成 JavaScript基础分为三个部分: ECMAScript:JavaScript的语法标准.包括变量.表达式.运算符.函数. ...

  9. Servlet的5种方式实现表单提交

    http://www.cnblogs.com/zhanghaoliang/p/5622900.html

  10. python scrapy同时执行spiders多个爬虫

    假设spiders文件夹下多个文件: name.py     name = 'name' name1.py    name = 'name1' name2.py    name = 'name2' . ...