#goroutines

109 messages · Page 1 of 1 (latest)

obtuse delta
#

will this ensure than if a stop command is sent the goroutine will stop executing

humble orchid
#

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.

obtuse delta
humble orchid
#

a for with a range over the channel

obtuse delta
#

is this right?

humble orchid
#

more idiomatic indeed, and you don't need the Command

knotty quartz
#

And why do you have a channel that just says that a function should be called?

#

Why not send the words over the channel?

humble orchid
#

it looks a bit convoluted, given the number of words in the end is the number of messages you read form the channel

obtuse delta
#

ig i can change it to bool so its like pulsing

obtuse delta
# knotty quartz And why do you have a channel that just says that a function should be called?
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

obtuse delta
humble orchid
#

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.

obtuse delta
humble orchid
#

size does not matter

knotty quartz
#

What is command handler supposed to do?

#

Why is it run concurrently?

obtuse delta
#

when commanded to

obtuse delta
humble orchid
#

it is

obtuse delta
humble orchid
#

I did not misunderstand, I'm answering, you need to fix the synchronization

knotty quartz
#

You must not read and write to the same memory without ensuring that the write happens in in isolation to reads.

humble orchid
#

if we knew how generated is going to be used, maybe we could give a possible way to synchronize it

knotty quartz
#

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.

obtuse delta
knotty quartz
#

What is the project supposed to do?

humble orchid
#

oof, I don't have enough time to read through a whole project

knotty quartz
#

You should add a README.md that explains what the project is for and what it does.

obtuse delta
obtuse delta
knotty quartz
#

Awful README. 😦

#

Can you explain what you're actually trying to do?

obtuse delta
#

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

knotty quartz
#

What does nextWord() string do?

obtuse delta
knotty quartz
#

What does it do though?

#

The name and return type explains what you just said.

#

What does the function actually do?

obtuse delta
#

Or if it's not arranged by frequency it just gives a random one

#

That's y the if else

knotty quartz
#

What does nextWord look like?

obtuse delta
#

The result?

knotty quartz
#

The code

obtuse delta
knotty quartz
#

Is it a text generator?

obtuse delta
knotty quartz
#

I see

#

How do you know when to stop?

obtuse delta
knotty quartz
#

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.

obtuse delta
knotty quartz
#

No, the write should close it

obtuse delta
#

Coz writer won't have access to timer

knotty quartz
#

It's much easier that way

#

Why not?

#

Can't you send a signal to the writer?

obtuse delta
knotty quartz
#

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.

obtuse delta
#

Ohh

knotty quartz
#

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.

obtuse delta
#

But

for range chnl {
    // ...
}

Will automatically stop if the channel gets closed?

knotty quartz
#

Yes

#

for word := range ch { will loop until the channel is closed.

obtuse delta
knotty quartz
#

Exactly

#

When does the writer know that it's time to send the next word?

obtuse delta
knotty quartz
#

No

#

The loop will read until it's closed.

obtuse delta
knotty quartz
#

Once the loop ends, the channel is closed already

#

So the reader asks for next word? 😐

obtuse delta
knotty quartz
#

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

obtuse delta
knotty quartz
#

The reader can run concurrently

obtuse delta
#

But then how will I sync the UI rendering

knotty quartz
#

What does the UI show?

obtuse delta
knotty quartz
#

It's too much code for me to read now.

#

Yeah

#

Why can't the reader update the UI?

obtuse delta
knotty quartz
#

When do you want to update it then?