用Lua模拟并行性
2016-11-18 14:26:37
收藏:0
阅读:68
评论:1
我正在尝试使用Lua来原型化一些并行算法。我指的是用纯Lua编写代码,在其中执行测试、调试等操作。当我有信心它可以运行时,我可以将其转换为真正的多线程库,甚至转换为另一种语言(如OpenCL内核)。显然,我不关心原型代码的性能。
我想使用一个在每行中都yield的协程,并加入一些样板代码来随机选择下一个“线程”运行。例如:
local function parallel_simulation(...)
local function_list = {...}
local coroutine_list = {}
local thread_number = #function_list
for i = 1, thread_number do
coroutine_list[i] = coroutine.create(function_list[i])
end
while 0 < thread_number do
local current = math.random(1, thread_number)
local worker = coroutine_list[current]
coroutine.resume(worker)
if 'dead' == coroutine.status(worker) then
thread_number = thread_number - 1
table.remove(coroutine_list, current)
end
end
end
----------------------------------------------------------
-- Usage example
local Y = coroutine.yield
local max = 3
local counter = 0
local retry = 99
local function increment()
Y() local c = counter
Y() while max > c do
Y() c = counter
Y() c = c + 1
Y() counter = c
Y() end
end
for i=1,retry do
counter = 0
parallel_simulation(increment, increment)
if max ~= counter then
print('Test SUCCESS ! A non-thread-safe algorithm was identified .', i, counter)
return
end
end
error('Test FAIL ! The non-thread-safe algorithm was not identified .')
这只是一个想法,任何涉及纯Lua的解决方案都受欢迎!这个解决方案让我非常不舒服,原因是所有的Y()。有没有什么方法可以避免它们?(debug.sethook不允许yield...)
第一次编辑-提供了更有意义的示例
编辑2-希望我清楚地表述了我想要实现的内容
点赞
评论区的留言会收到邮件通知哦~
推荐文章
- Lua 虚拟机加密load(string.dump(function)) 后执行失败问题如何解决
- 我想创建一个 Nginx 规则,禁止访问
- 如何将两个不同的lua文件合成一个 东西有点长 大佬请耐心看完 我是小白研究几天了都没搞定
- 如何在roblox studio中1:1导入真实世界的地形?
- 求解,lua_resume的第二次调用继续执行协程问题。
- 【上海普陀区】内向猫网络招募【Skynet游戏框架Lua后端程序员】
- SF爱好求教:如何用lua实现游戏内调用数据库函数实现账号密码注册?
- Lua实现网站后台开发
- LUA错误显式返回,社区常见的规约是怎么样的
- lua5.3下载库失败
- 请问如何实现文本框内容和某个网页搜索框内容连接,并把网页输出来的结果反馈到另外一个文本框上
- lua lanes多线程使用
- 一个kv数据库
- openresty 有没有比较轻量的 docker 镜像
- 想问一下,有大佬用过luacurl吗
- 在Lua执行过程中使用Load函数出现问题
- 为什么 neovim 里没有显示一些特殊字符?
- Lua比较两个表的值(不考虑键的顺序)
- 有个lua简单的项目,外包,有意者加微信 liuheng600456详谈,最好在成都
- 如何在 Visual Studio 2022 中运行 Lua 代码?

一个将
Y()放在每一行前面的简单替代方法是使用gsub和load:Y = coroutine.yield max = 3 counter = 0 code = [[ function increment() local c = counter while max > c do c = counter c = c + 1 counter = c end end]] code = code:gsub("\n ", "\n Y() ") -- 将 find/replace 中的两个空格替换为您使用的任何 Tab 字符。 assert(load(code))() local retry = 99 -- 其余代码在此处(根据您的 Lua 版本使用
load或loadstring) 请注意,变量声明 Y/max/counter 必须为全局变量,否则加载的函数将无法访问它们。类似地,code中的函数必须是全局的,否则increment将不存在于加载的代码之外。当然,这样的解决方案假设每行上的所有指令都是原子/线程安全的。
我建议对
parallel_simulation进行改进,以添加一些改变下一个线程选择方式的方法,例如,也许仅当一个线程在执行初期而另一个线程即将完成时才会显示错误 - 虽然通过充分的随机试验可以达到这种状态,但添加一个允许您调整哪些线程更有可能被选择的参数(例如使用权重)应该可以使它更加可能发生。