- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我正在编写此示例代码来自学如何跨并行 goroutine 共享 channel ,但我遇到了竞争条件。该程序应该启动与系统上可用的 CPU 一样多的 goroutine。第一个访问 bl channel 的 goroutine 立即将 channel 设置为包含 false,这样就没有其他 goroutine 可以访问范围超过 st channel 的循环。其他 goroutine 应该在第一个访问 bl channel 的 goroutine 从 st channel 读取并打印每个值时结束。
package main
import (
"fmt"
"runtime"
"strconv"
"math/rand"
"time"
"sync"
)
func main () {
runtime.GOMAXPROCS(runtime.NumCPU())
var wg sync.WaitGroup
st := make(chan string)
bl := make(chan bool)
go func() {bl <- true}()
for i := 0; i < runtime.NumCPU(); i++ {
wg.Add(1)
go func(x int) {
defer wg.Done()
fmt.Println("me: ", strconv.Itoa(x))
can := <- bl
bl <- false
if can {
for val := range st {
t := strconv.Itoa(rand.Int()%3)+"s"
dur, _ := time.ParseDuration(t)
time.Sleep(dur)
fmt.Println("time: ", t," i: ", strconv.Itoa(x), val)
}
}
fmt.Println("done: ", strconv.Itoa(x))
}(i)
}
for i := 0; i < 10; i++ {
st <- "n: "+strconv.Itoa(i)
}
wg.Wait()
close(st)
}
当我运行代码时,出现以下错误:
$ go run share.go
me: 1
me: 0
me: 2
me: 3
done: 0
done: 2
time: 2s i: 1 n: 0
time: 1s i: 1 n: 1
time: 0s i: 1 n: 2
time: 2s i: 1 n: 3
time: 2s i: 1 n: 4
time: 2s i: 1 n: 5
time: 0s i: 1 n: 6
time: 2s i: 1 n: 7
time: 2s i: 1 n: 8
time: 2s i: 1 n: 9
fatal error: all goroutines are asleep - deadlock!
goroutine 1 [semacquire]:
sync.runtime_Semacquire(0xc4200701bc)
/usr/local/go/src/runtime/sema.go:47 +0x30
sync.(*WaitGroup).Wait(0xc4200701b0)
/usr/local/go/src/sync/waitgroup.go:131 +0x97
main.main()
/share.go:80 +0x1ef
goroutine 36 [chan receive]:
main.main.func2(0xc4200701b0, 0xc42006e0c0, 0xc42006e060, 0x1)
/share.go:64 +0x23e
created by main.main
/share.go:73 +0x127
goroutine 38 [chan send]:
main.main.func2(0xc4200701b0, 0xc42006e0c0, 0xc42006e060, 0x3)
/share.go:61 +0x1ef
created by main.main
/share.go:73 +0x127
exit status 2
我不确定如何处理 bl channel 的竞争条件。似乎最后一个 goroutine 在尝试从 bl channel 读取时卡住了,但没有任何内容可供读取,因为它之前的 goroutine 尚未插入 false
然而。不确定我的直觉是否正确。我在代码周围放置了打印行,这似乎至少是正在发生的事情。我还尝试在 bl channel 周围放置一个 if,但最终出现了同样的问题。我也动了bl <- false
在if can {
里面 block ,但这也不起作用。我该如何解决这个问题?
最佳答案
您可以通过 channel 发送 channel 以保护所选值的单一访问。只有一个接收者会做这项工作,其他人在收到关闭信号后就会离开。参见 this solution
关于跨并行 goroutine 共享 channel 时的 Golang 竞争条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41756064/
我很好奇为什么以下不起作用。一般select用default:防止死锁,但在这种情况下不是: package main import "fmt" func main () { a := mak
我一生都无法弄清楚如何切换图像排序。图像以 (x,x,3) 格式读取,theano 要求它是 (3,x,x) 格式。我尝试更改顺序numpy.array([img[:,:,i] for i in ra
我正在向 libnl 发送单个 SSID 和频率进行扫描,但我得到了多个扫描结果以及我请求的 SSID 和频率,但我需要单个扫描结果(仅适用于请求的 SSID),如何实现这一点。请帮助我,我也发送了我
我是 Golang 的新手,但正在努力理解这门伟大的语言!请帮帮我.. 我有 2 个 channel 。 “进”和“出” channel in, out := make(chan Work),
例如我有这段代码: package main import ( "fmt" ) func main() { c1 := make(chan interface{}) close
我们使用以下调用来获取经过身份验证的用户的 ChannelID,它适用于大多数情况。一些 YouTube 用户将他们的 channel 连接到 Google+ 信息页,但在这种情况下,我们的一位用户无
case 'sinfo': const sinfo = new Discord.MessageEmbed() .addField('Server Name 🔎 :', message.guild.n
我需要让所有 channel 来创建一个 bunker 命令,这使得所有 channel 都是只读的。 最佳答案 他们变了Client.servers至 Client.guilds在 newer ve
为什么当第二个值通过另一个 go routine 发送并且没有收到发送的第一个值时, channel c 没有缓冲? package main import "fmt" func sum(s []in
据我所知,内置的 split 会将一个 3 channel Mat 拆分为三个 1 channel Mat。结果,这三个 Mat 只是具有一些不同强度的灰度。 我的意图是获得三个 3 channel
如何检测当前的 RAM 配置?我需要询问 Windows RAM 当前是在单 channel 、双 channel 还是四 channel 中运行。 我搜索了很多,并没有在这个网站或其他网站上找到任何
我需要拆分一个多 channel wav 文件并将每个 channel 编码为 mp3 文件。 我知道 gtresamer 的 deinterleave 插件,但我不确定如何将它用于 wav 文件以及
关闭。这个问题需要details or clarity .它目前不接受答案。 想要改进这个问题吗? 通过 editing this post 添加详细信息并澄清问题. 关闭 8 年前。 Improve
我正在尝试运行 Hyperledger Fabric 网络,它由单个订购者、单个对等节点和一个 cli 组成。为了学习启动 Hyperledger Fabric 网络的过程,从创建与加密相关的工件到将
我在 Laravel 中使用事件广播。我正在使用基于角色的通知访问权限。我有用于广播的自定义 auth guard。当用户连接到 channel 时,客户端将具有内部权限的 access_token
我正在编写一个使用 Elixir Channels 来处理实时事件的应用程序。我知道每个客户端将打开 1 个套接字,并且可以在其上多路复用多个 channel 。所以我的应用程序是一个聊天应用程序,其
我有一些 .wav 文件,我想转换它们的频率 (fs) 和 channel 数 (nchannels)。我在jupyter笔记本python3.6上使用ffmpeg。我使用了以下命令并且它有效。 cm
我有一个视频渲染器,它需要两个 H265 流(YUV420),我需要烘焙它们以使它们中的一个与另一个形成 alpha 蒙版。这一切都已解决并且效果很好,但是如果我按照此处的说明进行操作: ffmpeg
我运行此命令以便能够将 udp 直播流传输到可使用正在构建的移动应用程序播放的 http 直播流。 它只是一个只有音频流的流。 ffmpeg -i udp://@localhost:1111 -map
我在我的 discord.js 机器人中创建了 nuke 命令,它创建了具有相同名称、权限、主题等的 channel ,并删除了“原始” channel 。但是有一个问题,如何使 channel 与“
我是一名优秀的程序员,十分优秀!