trying to understand golang chan cause crash or do something elseAdding a func never called improves behavior?Is blocking on a channel send a bad synchronization paradigm and whywhat would cause code blocking in go?What's the difference between c:=make(chan int) and c:=make(chan int,1)?Convert chan to non chan in golangGolang: Why does increasing the size of a buffered channel eliminate output from my goroutines?Golang - Range over chan map by keyWhy can I not use func literal as custom func type despite matching parameters in golang?Multiple go routines consuming from a channel causing loss of datareturn early from range of a channel
Attempting to shape traffic for both IPv4 and IPv6 results in a conflict
Non-OR journals which regularly publish OR research
How to avoid the "need" to learn more before conducting research?
What is Poincare's "Fourth Geometry"?
Withdrew when Jimmy met up with Heath
Ex-contractor published company source code and secrets online
MinionPro is erroneous
Why should we care about syntactic proofs if we can show semantically that statements are true?
Why isn’t SHA-3 in wider use?
Visa National - No Exit Stamp From France on Return to the UK
Y2K... in 2019?
Is refreshing multiple times a test case for web applications?
Simple Stop watch which i want to extend
Infeasibility in mathematical optimization models
How to create all combinations from a nested list while preserving the structure using R?
Why did the RAAF procure the F/A-18 despite being purpose-built for carriers?
Can you castle with a "ghost" rook?
Are differences between uniformly distributed numbers uniformly distributed?
Three legged NOT gate? What is this symbol?
What is the maximum number of PC-controlled undead?
What game uses dice with sides powers of 2?
(11 of 11: Meta) What is Pyramid Cult's All-Time Favorite?
Acceptable to cut steak before searing?
Is it okay for a ticket seller to grab a tip in the USA?
trying to understand golang chan cause crash or do something else
Adding a func never called improves behavior?Is blocking on a channel send a bad synchronization paradigm and whywhat would cause code blocking in go?What's the difference between c:=make(chan int) and c:=make(chan int,1)?Convert chan to non chan in golangGolang: Why does increasing the size of a buffered channel eliminate output from my goroutines?Golang - Range over chan map by keyWhy can I not use func literal as custom func type despite matching parameters in golang?Multiple go routines consuming from a channel causing loss of datareturn early from range of a channel
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
The following is a example from https://golang.org/ref/mem:
var c = make(chan int)
var a string
func f()
a = "hello, world"
<-c
func main()
go f()
c <- 0
print(a)
is also guaranteed to print "hello, world". The write to a happens before the receive on c, which happens before the corresponding send on c completes, which happens before the print.
If the channel were buffered (e.g., c = make(chan int, 1)) then the program would not be guaranteed to print "hello, world". (It might print the empty string, crash, or do something else.)
I understand that It might print the empty string, but not for crash or do something else, when will crash happen? And when will it do something else ?
go memory concurrency
add a comment |
The following is a example from https://golang.org/ref/mem:
var c = make(chan int)
var a string
func f()
a = "hello, world"
<-c
func main()
go f()
c <- 0
print(a)
is also guaranteed to print "hello, world". The write to a happens before the receive on c, which happens before the corresponding send on c completes, which happens before the print.
If the channel were buffered (e.g., c = make(chan int, 1)) then the program would not be guaranteed to print "hello, world". (It might print the empty string, crash, or do something else.)
I understand that It might print the empty string, but not for crash or do something else, when will crash happen? And when will it do something else ?
go memory concurrency
7
The behavior is undefined. Don't try to find logic in it. Read: Benign Data Races: What Could Possibly Go Wrong? And if not enogh: Golang data races to break memory safety
– icza
Mar 27 at 8:26
add a comment |
The following is a example from https://golang.org/ref/mem:
var c = make(chan int)
var a string
func f()
a = "hello, world"
<-c
func main()
go f()
c <- 0
print(a)
is also guaranteed to print "hello, world". The write to a happens before the receive on c, which happens before the corresponding send on c completes, which happens before the print.
If the channel were buffered (e.g., c = make(chan int, 1)) then the program would not be guaranteed to print "hello, world". (It might print the empty string, crash, or do something else.)
I understand that It might print the empty string, but not for crash or do something else, when will crash happen? And when will it do something else ?
go memory concurrency
The following is a example from https://golang.org/ref/mem:
var c = make(chan int)
var a string
func f()
a = "hello, world"
<-c
func main()
go f()
c <- 0
print(a)
is also guaranteed to print "hello, world". The write to a happens before the receive on c, which happens before the corresponding send on c completes, which happens before the print.
If the channel were buffered (e.g., c = make(chan int, 1)) then the program would not be guaranteed to print "hello, world". (It might print the empty string, crash, or do something else.)
I understand that It might print the empty string, but not for crash or do something else, when will crash happen? And when will it do something else ?
go memory concurrency
go memory concurrency
asked Mar 27 at 8:23
Geln YangGeln Yang
5392 gold badges15 silver badges35 bronze badges
5392 gold badges15 silver badges35 bronze badges
7
The behavior is undefined. Don't try to find logic in it. Read: Benign Data Races: What Could Possibly Go Wrong? And if not enogh: Golang data races to break memory safety
– icza
Mar 27 at 8:26
add a comment |
7
The behavior is undefined. Don't try to find logic in it. Read: Benign Data Races: What Could Possibly Go Wrong? And if not enogh: Golang data races to break memory safety
– icza
Mar 27 at 8:26
7
7
The behavior is undefined. Don't try to find logic in it. Read: Benign Data Races: What Could Possibly Go Wrong? And if not enogh: Golang data races to break memory safety
– icza
Mar 27 at 8:26
The behavior is undefined. Don't try to find logic in it. Read: Benign Data Races: What Could Possibly Go Wrong? And if not enogh: Golang data races to break memory safety
– icza
Mar 27 at 8:26
add a comment |
1 Answer
1
active
oldest
votes
A string in Go is a read only slice of bytes. Slice consists of length an pointer. Let's assume that we first set length to a large value and then change the pointer. The other go routine may first read new length and old pointer. Then it tries to read over the end of the previous string. It either read some garbage or is stopped by operating system and crashes.
The order of operations does not matter really, if you set pointer firsts it may point to memory area too short for the current length.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55372640%2ftrying-to-understand-golang-chan-cause-crash-or-do-something-else%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
A string in Go is a read only slice of bytes. Slice consists of length an pointer. Let's assume that we first set length to a large value and then change the pointer. The other go routine may first read new length and old pointer. Then it tries to read over the end of the previous string. It either read some garbage or is stopped by operating system and crashes.
The order of operations does not matter really, if you set pointer firsts it may point to memory area too short for the current length.
add a comment |
A string in Go is a read only slice of bytes. Slice consists of length an pointer. Let's assume that we first set length to a large value and then change the pointer. The other go routine may first read new length and old pointer. Then it tries to read over the end of the previous string. It either read some garbage or is stopped by operating system and crashes.
The order of operations does not matter really, if you set pointer firsts it may point to memory area too short for the current length.
add a comment |
A string in Go is a read only slice of bytes. Slice consists of length an pointer. Let's assume that we first set length to a large value and then change the pointer. The other go routine may first read new length and old pointer. Then it tries to read over the end of the previous string. It either read some garbage or is stopped by operating system and crashes.
The order of operations does not matter really, if you set pointer firsts it may point to memory area too short for the current length.
A string in Go is a read only slice of bytes. Slice consists of length an pointer. Let's assume that we first set length to a large value and then change the pointer. The other go routine may first read new length and old pointer. Then it tries to read over the end of the previous string. It either read some garbage or is stopped by operating system and crashes.
The order of operations does not matter really, if you set pointer firsts it may point to memory area too short for the current length.
edited Mar 27 at 9:48
answered Mar 27 at 8:36
Grzegorz ŻurGrzegorz Żur
33.2k13 gold badges85 silver badges82 bronze badges
33.2k13 gold badges85 silver badges82 bronze badges
add a comment |
add a comment |
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55372640%2ftrying-to-understand-golang-chan-cause-crash-or-do-something-else%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
7
The behavior is undefined. Don't try to find logic in it. Read: Benign Data Races: What Could Possibly Go Wrong? And if not enogh: Golang data races to break memory safety
– icza
Mar 27 at 8:26