- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
有没有办法让 lua channel 线程进行通信或从外部访问线程?
不使用文档提供的繁忙循环。
一个简单的例子是,一个线程使用一个变量,更新它,改变它等等,另一个线程或主程序能够访问/获取该变量。
这可以用 lua channel 吗?
我的意思是纯粹在 lua 中而不是在 c/c++ 中。
最佳答案
在使用多线程时,您通常 do not want to "update/change" a variable from multiple threads without any synchronization - 这可能会导致由于对变量/表等的不同步访问而导致随机出现的错误。
相反,您应该依靠消息传递来处理线程之间的通信。这称为 actor model并且直接受到某些语言的支持,例如 Erlang。
LuaLanes也接受这种沟通模式。为了在各个 channel 之间进行通信,您创建了一个 Linda对象,可以由主线程和派生线程共享,并用于通信。 Linda 对象为您处理同步,您不需要锁定。每个操作(发送、接收消息)都是原子的。
Without using busy loops...
虽然看起来像,但在 LuaLanes 中没有繁忙的循环。如果您尝试在没有值的键上 linda:receive()
,LuaLanes puts the reading thread on a wait直到 linda 对象被修改。所以线程处理消息的一般方式如下:
local lanes = require "lanes"
local linda = lanes.linda()
local thread = lanes.gen("*", function()
print("Starting thread...")
while true do
local command = linda:receive("cmd")
if command=="quit" then
break
else
-- do processing based on command
end
end
end)
local threads = {}
local NCORES = 4
for i=1,NCORES do threads[i] = thread() end -- start the threads
-- send messages, like files for processing, using linda:send("cmd", {file=..., op=...})
for i=1,NCORES do linda:send("cmd", "quit") end -- send the quit command
for i=1,NCORES do threads[i]:join() end -- wait for the threads to end
关于windows - lua lane 线程间通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9572317/
这是我编写的用于生成简单 .bpmn 文件的代码片段 public void testDynamicDeploy() throws Exception { BpmnModel model
有没有办法让 lua channel 线程进行通信或从外部访问线程? 不使用文档提供的繁忙循环。 一个简单的例子是,一个线程使用一个变量,更新它,改变它等等,另一个线程或主程序能够访问/获取该变量。
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
非常简单的标记: Headline 01 Lorem ipsum dolor...
我正在使用 Totalview 尝试调试 CUDA 内核。我选择了在 CUDA 中调试内存错误的选项,并在主机出现内存错误时停止。 我在这条线上的一个内核中停止了: d_test_filt[i*col
我正在为 faSTLane 设置一个 Fastfile,我正在尝试打开 faSTLane 正在运行的 channel 名称。 假设我运行 faSTLane wow 在这个 Fastfile 中,我想获
当尝试从 Lua 模块调用 C 函数时,使用 Lua channel ,控制不会转移到“C”函数。 Lua channel 不能以线程方式与外部 C dll 一起工作有什么问题吗? 下面是代码片段 L
我确实理解以太坊区 block 链中气体的概念,以及为什么需要气体来补偿矿工为网络提供计算能力。然而,当我阅读 chainlink vrf 获取随机数文档时,它说 keyhash 告诉你要使用的加油道
我无法使用 faSTLane 构建 beta 应用程序并且错误说-: 1.AAPT:错误:找不到资源android:attr/fontVariationSettings。 2.AAPT:错误:找不到资
我是一名优秀的程序员,十分优秀!