R
R
Romses Panagiotis2021-01-20 01:46:32
go
Romses Panagiotis, 2021-01-20 01:46:32

Why does this code behave differently on multiple runs?

https://play.golang.org/p/AhFXtRGcVba

Sometimes I get this:

output

!! scanLogLines
SCAN: Emitter A 1
SCAN: Emitter A 2
SCAN: Emitter A 3
SCAN: Emitter A 4
SCAN: Emitter A 5
>> log SEND - [Emitter A 1 Emitter A 2 Emitter A 3 Emitter A 4 Emitter A 5]
SCAN: Emitter A 6
SCAN: Emitter A 7

<< log RCV  - [Emitter A 6 Emitter A 7 Emitter A 3 Emitter A 4 Emitter A 5]
SCAN: Emitter A 8
SCAN: Emitter A 9
SCAN: Emitter A 10
>> log SEND - [Emitter A 6 Emitter A 7 Emitter A 8 Emitter A 9 Emitter A 10]
SCAN: Emitter A 11
SCAN: Emitter A 12

<< log RCV  - [Emitter A 6 Emitter A 7 Emitter A 8 Emitter A 9 Emitter A 10]
SCAN: 1=========================
>> log SEND - [Emitter A 11 Emitter A 12 1=========================]
!! /scanLogLines

<< log RCV  - [Emitter A 11 Emitter A 12 1=========================]


In theory, "log RCV" should duplicate "log SEND"

It is noteworthy that in the Go Playground it works as it should, until you change the delay to 0 ns.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Tikhonov, 2021-01-20
@romesses

slice contains a pointer inside, which leads to races during slice parallel processing in different goroutines

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question