我正在学习 Go 中的并发模式,不确定点 A 的目的是什么?
代码取自:https://talks.golang.org/2012/concurrency.slide#30
谁能给我解释一下?谢谢
type Message struct {
str string
wait chan bool
}
func main() {
c := fanIn(boring("Joe"), boring("Ann"))
for i := 0; i < 5; i++ {
msg1 := <-c
fmt.Println(msg1.str)
msg2 := <-c
fmt.Println(msg2.str)
msg1.wait <- true
msg2.wait <- true
}
fmt.Println("You're both boring; I'm leaving.")
}
func fanIn(input1, input2 <-chan Message) <-chan Message {
c := make(chan Message)
go func() {
for {
c <- <-input1
}
}()
go func() {
for {
c <- <-input2
}
}()
return c
}
func boring(msg string) <-chan Message {
waitForIt := make(chan bool)
c := make(chan Message)
go func() { // We launch the goroutine from inside the function.
for i := 0; ; i++ {
c <- Message{fmt.Sprintf("%s %d", msg, i), waitForIt}
time.Sleep(time.Duration(rand.Intn(2e3)) * time.Millisecond)
<-waitForIt // <-----------------Point A
}
}()
return c // Return the channel to the caller.
}
最佳答案
fanIn 产生两个 goroutines 从第一个和第二个“无聊的”消息 channel 读取数据。由于两个 goroutine 中的任何一个都可能正在运行(另一个 goroutine 也在休眠或运行),我们不知道元素写入统一 channel 的顺序:
A A A A A \
> A B B A A A B A B B
B B B B B /
请注意,生成的序列没有特定顺序。
这可以通过 commenting out the "waiting"-channels 来证明:
Joe 0
Ann 0
Joe 1
Ann 1
Joe 2
Ann 2
Joe 3
Joe 4
Joe 5
Ann 3
You're both boring; I'm leaving.
等待 channel 的存在是为了恢复您链接的幻灯片所说的顺序。我们想要这个:
A A A A A \
> A B A B A B A B A B
B B B B B /
请注意 Ann 和 Joe 如何 talk one after the other now :
Joe 0
Ann 0
Joe 1
Ann 1
Joe 2
Ann 2
Joe 3
Ann 3
Joe 4
Ann 4
You're both boring; I'm leaving.
这段代码的作者决定通过让它们在 waitForIt 上等待并在 main 中通知来同步对 channel 的写入。此行为依赖于我们在 main 的 for 循环中获得的前两条消息是否有序。如果不是,我们将得到相反的序列。
也许我没有看到任何更多的绝对同步保证,也许作者不关心序列的顺序,只要它是一个重复序列,但这段代码并没有让我觉得是一个特别好的例子并发和同步。
关于转到 channel : the function of one extra line,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26115537/
我正在使用Maruku,将Markdown(超集)转换为HTML,你知道我该怎么做才能从HTML转换为Markdown吗? 最佳答案 Google发现了一个名为reverse_markdown的ruby脚本.它似乎可以满足您的需求。 关于ruby-on-rails-我需要从HTML转到markdown,有什么建议吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/175162
有没有办法使用vim结束Rubyblock?例如moduleSomeModule#defsome_methodendend我想用一个命令从光标所在的位置移动到block的末尾,这可能吗?我读过thisdocumentation,但它似乎不适用于.rb文件,我在某些地方读到它只适用于C(虽然还没有尝试过)。提前致谢。 最佳答案 rubyforge好像有官方包对此有一些支持:TheRubyftpluginnowincludesRubyspecificimplementationsforthe[[,]],[],][,[m,]m,[M,an
我在使用ExhuberantCtags跳转到Rubybang方法时遇到问题。我已经搜索过其他有类似问题的人,但找不到任何东西。可以使用以下小型Ruby类显示该问题的示例:classHellodefstartmethod!enddefmethod#Blahenddefmethod!#Blahendend当ctags-R.在此文件上运行时,生成的tags文件包含以下两行,表明这两种方法都是在生成时发现的:methodtest.rb/^defmethod$/;"fclass:Hellomethod!test.rb/^defmethod!$/;"fclass:Hello但是,如果我将光标放
更新:我想通了。Ctrl-F仅在未选择我正在搜索的方法时有效。游标只需要在方法名中。我刚升级到TextMate2。当我选择一个方法并使用Ctrl+F转到它的定义时,我得到:>FailurerunningJumptoMethodDefinition这是痕迹:/Users/ilikepie/Library/ApplicationSupport/TextMate/Managed/Bundles/RubyonRails.tmbundle/Support/lib/rails/text_mate.rb:54:in`method_missing':undefinedmethod`current_li
我有一个订阅用户的ActionCable方法。如果开始新的session,我也想为用户订阅新channel。我想不出在Controller中调用channel方法的正确语法。更新:问题是消息在发送时附加到聊天框,但是当发送第一条消息时,websocket连接尚未建立,因此在用户看来好像消息没有发送(因为它没有被附加)。channel/msgs_channel.rbclassMsgsChannel在我的convosController中,create方法,我尝试了几种方法:convos_controller.rbdefcreate@convo=Convo.create!({sender_
我正在尝试创建一个公开ActionCablechannel的gem,但我无法让它工作。这是我的宝贝#lib/my_channel.rbclassMyChannel然后我将gem添加到我的主要应用程序Gemfile,运行bundleinstall,启动控制台并运行MyChannel。没有屈服和错误,这意味着channel已正确包含。然后我将其添加到我的主应用程序//application.jsvarsocket="ws://localhost:3000/cable";varcable=ActionCable.createConsumer(socket);cable.subscriptio
我正在使用vim进行ruby、php和perl开发。有快捷方式%可以从block(子例程/函数/方法/if)的开头跳转到结尾,反之亦然。对我来说,ruby中的do/end标记上的%不起作用。我如何用vim做到这一点? 最佳答案 matchitplugin允许匹配的不仅仅是括号和注释。可以找到ruby版本here. 关于ruby-如何使用Vim从do跳转到Rubyblock的末尾?,我们在StackOverflow上找到一个类似的问题: https://
我可以从正在运行的Ruby脚本转到IRB提示吗?我想运行一个脚本,然后让它在程序中的某个点给我一个IRB提示以及程序的当前状态,但不仅仅是通过运行rdebug和设置断点。 最佳答案 Pry(一个IRB替代方案)也可以让你这样做,事实上它是为这个用例从头开始设计的:)这就像将binding.pry放在您想要开始session的位置一样简单:require'pry'x=10binding.pry在session中:pry(main)>putsx=>10查看网站:http://pry.github.com请让我们:在您的代码中的任何一点进
我正在尝试实现一个具有两个输入channel和一个输出channel的ScriptProcessorNode。varsource=newArray(2);source[0]=context.createBufferSource();source[0].buffer=buffer[0];source[1]=context.createBufferSource();source[1].buffer=buffer[1];vartest=context.createScriptProcessor(4096,2,1);source[0].connect(test,0,0);source[1].c
在我的工作文件中,我监听数据回调。someLib是Node串口。process.on('message',function(msg){someLib.on('data',function(data){console.log('somedata');process.send(data);});});这打印somedataError:channelclosed但是process.on('message',function(msg){process.send('foobar');});工作正常。这很奇怪,但有时第一个代码示例有效,所以channel关闭错误随机出现。来自http://node