#goroutines
109 messages · Page 1 of 1 (latest)
Yeah, it looks so.
It would be more idiomatic if the for was directly on the read from the channel, and if the end of the stream was signaled by a closed channel, a la for _ = range cmd.
for was directly on the read from the channel
wdym? i dont understand
a for with a range over the channel
yc explained it, and i implemented it
func commandHandler(cmd chan struct{}, generated *[]string, nextWord func() string) {
for range cmd {
*generated = append(*generated, nextWord())
}
}
is this right?
more idiomatic indeed, and you don't need the Command
Why did you change the type from Command to struct{}?
And why do you have a channel that just says that a function should be called?
Why not send the words over the channel?
it looks a bit convoluted, given the number of words in the end is the number of messages you read form the channel
coz now theres only 1 command left
ig i can change it to bool so its like pulsing
func generateTimeTest(nextWord func() string, cmd chan struct{}) *[]string {
var generated []string
for range INIT_WORDS {
generated = append(generated, nextWord())
}
go commandHandler(cmd, &generated, nextWord)
return &generated
}
func commandHandler(cmd chan struct{}, generated *[]string, nextWord func() string) {
for range cmd {
*generated = append(*generated, nextWord())
}
}
this is the full code
as the user types more words, more words get generated
i'll fix the naming a little
coz ig then the reader will be blocking?
The goroutine running comamndHandler() is reading and writing the generated string array, and the main goroutine is gonna read that same variable, and there is no synchronization of any kind.
ig its fine coz theres a buffer of 100 words
size does not matter
add more words to the slice
when commanded to
and there is no synchronization of any kind
y is it necessary
it is
no im not challenging ur im asking y
I did not misunderstand, I'm answering, you need to fix the synchronization
You must not read and write to the same memory without ensuring that the write happens in in isolation to reads.
if we knew how generated is going to be used, maybe we could give a possible way to synchronize it
What problem will it cause?
Corruption
Imagine you ready a piece of text just as someone is overwriting it. How much of the new and old text will you know?
You'll get some really weird bugs.
What is the project supposed to do?
oof, I don't have enough time to read through a whole project
See the test folder
It's in a pr
Soon
Generator and test.go files
Uhh so it's a typing test app
And the words are generated infinitely so it keeps going
After u type one word the slice reader sends command to add one more word
And it starts with 100 words
So there's a 100 words buffer in between
What does nextWord() string do?
Gives the next word to add
What does it do though?
The name and return type explains what you just said.
What does the function actually do?
The list of words are arranged in frequency order so it selects one randomly but giving the more frequent ones more frequently
Or if it's not arranged by frequency it just gives a random one
That's y the if else
What does nextWord look like?
The code
U can see the code in GitHub repo?
Is it a text generator?
No it picks a word from predefined set of words
I haven't implemented that yet but it should stop when timer runs out
Send the words through a channel and the writer can close the channel when time's up
The reader of the channel will notice that there's no more words coming and can return.
Well the reader will have to close and the writer will stop
No, the write should close it
Coz writer won't have access to timer
Yes ig
It would be much easier and less error prone that way
Reading from a closed channel yields a zero value. Writing to a closed channel causes a panic.
Ohh
So you want the writer be in control of that
You can use context.Context to tell the writing that it's time to stop.
The writer will check the context before writing. If the context is cancelled, it can close the channel.
The reader will notice that the channel is closed.
But
for range chnl {
// ...
}
Will automatically stop if the channel gets closed?
So no extra writes an no panic?
So it's safe for reader to close in this case?
Reader signals it, after one word is typed
Once the loop ends, the channel is closed already
So the reader asks for next word? 😐
Yes
Then the reader should just loop and call a function to get the next and then stop calling it when it's time to stop.
No channel
But that'll block the main thread while all the operation is done
The reader can run concurrently
But then how will I sync the UI rendering
What does the UI show?
The words ?
I can't just update one single word in the UI I have to recalculate the entire UI and print it
When do you want to update it then?