- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
遇到所有 goroutine 都处于休眠状态的问题 - 死锁。
我有一个带有汽车数组的数据结构,其中的汽车数量有限。如果数组中没有汽车并且主线程尚未完成将数据写入数据结构,则工作线程启动并尝试从数据结构中删除汽车,然后工作线程休眠,直到主线程将更多汽车添加到数据结构汽车数组中。然后工作线程唤醒,从数据结构中删除汽车对象,进行计算并将其移动到结果结构中。在某些时候,它有时会陷入僵局。注意到即使程序毫无异常(exception)地完成,一些数据(有时更多,有时更少)仍然丢失。
代码:
package main
import (
"crypto/sha256"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"strconv"
"sync"
)
type Car struct {
Make string `json:"Make"`
Year int `json:"Year"`
Displacement float64 `json:"Displacement"`
Hash string
}
type Cars struct {
Auto []Car
count int
MaxLen int
mutex *sync.Mutex
cond *sync.Cond
end bool
}
func (a *Cars) Insert(aut Car) {
a.mutex.Lock() // lock method so other thread couldin't use Data structure
for a.count == a.MaxLen {
a.cond.Wait()//wait if current count of cars is equal to maximum amount that are allowed to store in cars array in Data structure
}
a.Auto[a.count] = aut
a.count++
a.mutex.Unlock()
a.cond.Broadcast()
}
func (a *Cars) Remove(group *sync.WaitGroup) Car {
a.mutex.Lock()
for a.count == 0 {
a.cond.Wait()//if there is no cars to remove from Data struct car array then sleep
}
result := a.Auto[a.count-1]//get the last car from cars structure car array
var tmp Car
a.Auto[a.count-1] = tmp//remove the last car from structure car array
a.count--
a.mutex.Unlock() // unlock this method and let others thread use i
a.cond.Broadcast() //tell all threads that removing has been finishedt
return result
}
func (a *Cars) InsertSort(aut Car) {
a.mutex.Lock()
for a.count == a.MaxLen {
a.cond.Wait()
}
j := 0
for i := 0; i < a.count; i++ {
if a.Auto[i].Displacement < aut.Displacement {
j = i //Finds where to insert new item in sorted list
}
}
if j != 0 {
for i := a.count; i >= j; i-- {
a.Auto[i+1] = a.Auto[i]//moves objects from j to the right
}
}
a.Auto[j] = aut
a.count++
a.mutex.Unlock()
a.cond.Broadcast()
}
var Auto []Car
func main() {
CurrentWD, err := os.Getwd()
if err != nil {
log.Println(err)
}
path := CurrentWD + "\\Auto.json"
jsonFile, err := os.Open(path)
byteValue, _ := ioutil.ReadAll(jsonFile)
json.Unmarshal(byteValue, &Auto)
var mutex = sync.Mutex{}
var cond = sync.NewCond(&mutex) //syncing cond with mutex
MaxLength := 5 // max lenght of data array
var A = make([]Car, 5)
Auto1 := Cars{count: 0, MaxLen: MaxLength, cond: cond, mutex: &mutex, Auto: A}//data structs
var B = make([]Car, 40)
Auto2 := Cars{count: 0, MaxLen: 40, cond: cond, mutex: &mutex, Auto: B}//results struct
var waitGroup = sync.WaitGroup{}
ThreadsAmt := 8
waitGroup.Add(ThreadsAmt)
for i := 0; i < ThreadsAmt; i++ {
go execute(&Auto1, &waitGroup, &Auto2)
}
for _, s := range Auto {
Auto1.Insert(s)
}
Auto1.end = true//finished writing to data struct
waitGroup.Wait()
var RLoc = CurrentWD + "\\Results.txt"
f, err := os.Create(RLoc)
defer f.Close()
f.WriteString(fmt.Sprintf("%15s|%4s|%12s|%50s \n", "Make", "Year", "Displacement", "Hash"))
for i := 0; i < Auto2.count-1; i++ {
f.WriteString(fmt.Sprintf("%3d %15s|%4d|%12.2f|%50s \n", i, Auto2.Auto[i].Make,
Auto2.Auto[i].Year, Auto2.Auto[i].Displacement, Auto2.Auto[i].Hash))
}
fmt.Println("Program finished execution")
}
func execute(Data *Cars, group *sync.WaitGroup, res *Cars) {
hash := sha256.New()
for Data.end == false && Data.count != 0 {
carTemp := Data.Remove(group)//removes and returns car object from data struct
if carTemp.Displacement > 0 {//checks if returned car object displacement is bigger than *
var ss string
ss = carTemp.Make + strconv.Itoa(carTemp.Year) + fmt.Sprint(carTemp.Displacement) //making string calculating hash
sum := hash.Sum([]byte(ss))
for i := 0; i < len(sum); i++ {
ss += string(sum[i])//joining hash byte array in to string
}
carTemp.Hash = ss
res.InsertSort(carTemp) // inserts car
}
}
defer group.Done()
}
数据:Auto.json
[{
"Make": "Chrysler",
"Year": 1997,
"Displacement": 3.6
}, {
"Make": "Honda",
"Year": 2016,
"Displacement": 1.4
}, {
"Make": "Aston Martin",
"Year": 2009,
"Displacement": 4.1
}, {
"Make": "Geo",
"Year": 2011,
"Displacement": 4.9
}, {
"Make": "Buick",
"Year": 2001,
"Displacement": 6.3
}, {
"Make": "Chevrolet",
"Year": 2001,
"Displacement": 2.7
}, {
"Make": "Suzuki",
"Year": 2004,
"Displacement": 4.5
}, {
"Make": "Studebaker",
"Year": 2001,
"Displacement": 7.5
}, {
"Make": "Chevrolet",
"Year": 2020,
"Displacement": 1.1
}, {
"Make": "Volkswagen",
"Year": 1996,
"Displacement": 6.2
}, {
"Make": "Mercedes-Benz",
"Year": 2009,
"Displacement": 2.9
}, {
"Make": "Nissan",
"Year": 2019,
"Displacement": 7.2
}, {
"Make": "Subaru",
"Year": 2010,
"Displacement": 2.6
}, {
"Make": "Hummer",
"Year": 1991,
"Displacement": 8.8
}, {
"Make": "Subaru",
"Year": 2017,
"Displacement": 8.0
}, {
"Make": "Mitsubishi",
"Year": 2010,
"Displacement": 6.6
}, {
"Make": "Mercedes-Benz",
"Year": 1996,
"Displacement": 2.0
}, {
"Make": "Lincoln",
"Year": 1991,
"Displacement": 9.9
}, {
"Make": "Chevrolet",
"Year": 1998,
"Displacement": 3.4
}, {
"Make": "Dodge",
"Year": 2010,
"Displacement": 5.8
}, {
"Make": "GMC",
"Year": 2016,
"Displacement": 6.8
}, {
"Make": "Chevrolet",
"Year": 2013,
"Displacement": 3.4
}, {
"Make": "Ford",
"Year": 2010,
"Displacement": 5.1
}, {
"Make": "Toyota",
"Year": 2017,
"Displacement": 9.6
}, {
"Make": "Hyundai",
"Year": 2015,
"Displacement": 3.8
}, {
"Make": "Mercedes-Benz",
"Year": 2016,
"Displacement": 4.3
}, {
"Make": "Chevrolet",
"Year": 2019,
"Displacement": 2.2
}, {
"Make": "Dodge",
"Year": 2009,
"Displacement": 1.8
}, {
"Make": "Pontiac",
"Year": 2006,
"Displacement": 4.6
}, {
"Make": "Chevrolet",
"Year": 2008,
"Displacement": 9.2
}]
错误:
goroutine 1 [sync.Cond.Wait]:
runtime.goparkunlock(...)
E:/Program Files (x86)/Go projects/go1.15.2/src/runtime/proc.go:312
sync.runtime_notifyListWait(0xc00003c050, 0x3)
E:/Program Files (x86)/Go projects/go1.15.2/src/runtime/sema.go:513 +0x117
sync.(*Cond).Wait(0xc00003c040)
E:/Program Files (x86)/Go projects/go1.15.2/src/sync/cond.go:56 +0xa5
main.(*Cars).Insert(0xc00003c080, 0xc000014343, 0x3, 0x7e0, 0x401b333333333333, 0x0, 0x0)
c:/Users/Justas/OneDrive - Kaunas University of Technology/5
Semestras/Lygiagretusis programavimas/Lab1/main.go:32 +0x57
main.main()
c:/Users/Justas/OneDrive - Kaunas University of Technology/5
Semestras/Lygiagretusis programavimas/Lab1/main.go:109 +0x53c
exit status 2
Process exiting with code: 1 signal: false
最佳答案
欢迎来到 StackOverflow 和 Go 开发!围棋货币很强大——但也很难掌握。不要气馁!
问题:
sync
锁定 - execute()
函数没有 - 导致大量数据竞争条件go build --race
和标准错误将显示并发读/写发生的位置。阅读 more here 。 InsertSort
功能在很多边缘情况下被破坏 h.Sum(nil)
计算哈希 fmt
十六进制格式打印它们时,建议使用。 ( %x
) len()
函数检索 - 因此无需使用您自己的 count
字段 s = s[:len(s)-1]
sort.Slice
为你做繁重的工作 sync.Mutex
和任何自定义结构的需要:
关于multithreading - 所有goroutines死锁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64234386/
我有2个功能: function func1() while true do -- listen on connection end end function func2()
我的问题可能看起来很奇怪,但我想我正面临着 volatile 的问题。对象。 我写了一个这样实现的库(只是一个方案,不是真正的内容): (def var1 (volatile! nil)) (def
由于 maven 支持多线程构建,是否可以同时运行 Sonar 多线程? (例如 mvn sonar:sonar -T 4 ) 我运行了它,当模块报告成功时,它报告整个构建失败并返回 java.uti
我们正在启动一个网站,该网站在短时间内的交易量非常大。它基本上是在给票。该代码是用Java,Spring和Hibernate编写的。我想通过产生多个线程并尝试使用JUnit测试用例来获取票证来模仿高容
我正在尝试访问像素数据并将图像从游戏中的相机保存到磁盘。最初,简单的方法是使用渲染目标,然后使用RenderTarget-> ReadPixels(),但是由于ReadPixels()的 native
我们有以下系统: 用户数:〜500k 项目数:〜100k UserSimilarity userSimilarity = new TanimotoCoefficientSimilarity(dataM
也许这是一个经常出现的问题,但我需要根据我的上下文进行一些自定义。 我正在使用 Spring Batch 3.0.1.RELEASE 我有一个简单的工作,有一些步骤。一个步骤是这样的 block :
也许这是一个经常出现的问题,但我需要根据我的上下文进行一些自定义。 我正在使用 Spring Batch 3.0.1.RELEASE 我有一个简单的工作,有一些步骤。一个步骤是这样的 block :
我正在尝试使用PyBrain和Python的multiprocessing软件包在Python中训练神经网络。 这是我的代码(它训练了一个简单的神经网络来学习XOR逻辑)。 import pybrai
我有一个繁重的功能,不适合在主时间轴上执行(因为要花很长时间才能完成并使程序崩溃)。 因此我在air(as3)中搜索多线程,但是我发现的所有示例都说明了如何在worker中运行单独的swf文件。如何在
我想实现线程A 和线程B 并行运行并共享全局变量。 下面是用python编写的代码。我想在中执行相同操作Dart (我不想使用future等待,因为它正在等待其他线程完成或必须等待。) 大小写变量:
我的一个项目只适用于调试 DLL,而不适用于非调试 DLL。 在 Debug DLL 设置下发布项目有哪些注意事项?例如,是否丢失了某些优化? 如何通过将调试版本设置为非调试 DLL 来调试此项目?我
我正在尝试比较 Matlab 和 Julia 之间的速度和性能。我正在查看一个代码,该代码对承受给定负载的连续体结构进行拓扑优化。我正在查看的代码是公共(public)代码topopt88.m:htt
Serving Flask 应用程序“服务器”(延迟加载) 环境:生产警告:这是一个开发服务器。不要在生产部署中使用它。请改用生产 WSGI 服务器。 Debug模式:开启 在 http://0.0.
我对 PyQT 很陌生。我正在学习如何制作 Progressbar 并随着算法的进展对其进行更新。我已经能够制作一个使用此链接进行 self 更新的基本进度条:Python pyqt pulsing
我正在尝试指定在特定线程上运行任务,这样我就可以使用两个专用于“放入” channel 的耗时任务的线程,而其他线程则用于处理该任务。 我对如何将特定任务分配给特定线程感到困惑。我以为我可以使用类似
我正在编写一个软件,它对很多(潜在的大)图像进行大量图像操作/合成。 多线程有助于提高速度,但 QT 不允许同时在同一图像上使用多个 QPainter。 所以我必须在副本的每个线程中进行图像操作/合成
此脚本读取 url 文件以执行多线程 HTTP 请求。 如何使用带有 url 的数组来发出多线程请求? 我的阵列将有类似的东西: @array = ("https://example.com/xsd"
Java 文档声明了以下关于构造函数同步的内容: Note that constructors cannot be synchronized — using the synchronized keyw
我有一个程序,其中主线程创建了很多线程。它崩溃了,我正在调试核心文件。崩溃发生在其中一个子线程中。为了找到原因,我需要知道主线程是否还活着。有什么方法可以找出哪个线程是初始线程? 最佳答案 Is th
我是一名优秀的程序员,十分优秀!