T
T
to_east2018-03-18 19:42:21
go
to_east, 2018-03-18 19:42:21

Competitiveness a few questions?

I welcome everyone!
I'll ask, starting from a specific task. Here I need to upload a relatively large file, and scatter the received information over the structure so that it is synchronized and faster.
And I see this algorithm:
1) Take the total number of lines in the file, and split it into the number of streams, something like this:
https://play.golang.org/p/3DD453D1GNJ
2) Then start the streams with line intervals, so that each thread processes its own interval, and that it reads line by line.
And so, a question: What more rational method of synchronization to use to this task.
For example, there is a Data structure, you need to make it so that only one thread at a time can read and write to this structure. Do the lock in the methods or is it possible to somehow impose a lock on the structure itself? In the course of writing the post, I remembered that I just organized a pool of threads with queues in Python, in principle, it frees me from the fact that I posted the listing above, I probably will do the same with the pool.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrey Tsvetkov, 2018-03-18
@yellow79

You don't have to break anything. Create a channel into which you will write lines from the file line by line, also create workers that will read from this channel and do something with this line, at the end, transfer the result to another channel for final processing. Only one reader reads from this last channel and writes to your Data structure.
I threw an example , if anything, ask

T
to_east, 2018-03-18
@to_east

Andrey Tsvetkov ,
Ok, thanks for the support! Let me elaborate on your example for clarity.
<-- After some time -->
Unfortunately your example failed with a fatal error

fatal error: all goroutines are asleep - deadlock!

goroutine 1 [chan receive]:
main.main()
        c:/Users/chapplin/go/src/example1/test.go:59 +0x3cf

goroutine 18 [chan receive]:
main.writer(0xc04202c0c0, 0xc042050070)
        c:/Users/chapplin/go/src/example1/test.go:23 +0x5e
created by main.main
        c:/Users/chapplin/go/src/example1/test.go:37 +0xf0
exit status 2
Для продолжения нажмите любую клавишу . . .

But then, I commented out the last line
. The code worked without errors.
Here is an additional question, apparently sigfault in an infinite loop:
func writer(result <- chan string, done chan bool) {
  for line := range result {
    fmt.Println(line)
  }
  done <- true
}

An additional check may be needed, and break out of the loop, like this:
func writer(result <- chan string, done chan bool) {
  for line := range result {
        if (<- done) == true {
            break
        }
    fmt.Println(line)
  }
  done <- true
}

And at the end of the program, already do done <- true
And why is this done needed at all if it is defined and there is no explicit use?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question