使用通道来交替打印。

huangapple go评论70阅读模式
英文:

Go use channel to alternated print

问题

我想让我的代码打印出:

12AB34CD56EF78GH910IJ

但实际上它打印出:

12AB3456CDEF78910GHIJ

我很困惑,觉得这很奇怪。

package main

import (
    "fmt"
    "sync"
)

func main() {
    numdone := make(chan int)
    var wg sync.WaitGroup
    wg.Add(1)
    go func() {
        defer wg.Done()
        data := []byte("ABCDEFGHIJ")
        for i := 0; i < 10; i = i + 2 {
            <-numdone
            fmt.Printf("%c", data[i])
            fmt.Printf("%c", data[i+1])
        }
    }()

    wg.Add(1)
    go func() {
        defer wg.Done()
        for i := 1; i < 11; i = i + 2 {
            fmt.Printf("%d", i)
            fmt.Printf("%d", i+1)
            numdone <- i
        }
    }()

    wg.Wait()
}
英文:

I want my code to print:

12AB34CD56EF78GH910IJ

but it prints:

12AB3456CDEF78910GHIJ

I am so confused and I think it is strange.

package main

import (
    &quot;fmt&quot;
    &quot;sync&quot;
)

func main() {
    numdone := make(chan int)
    var wg sync.WaitGroup
    wg.Add(1)
    go func() {
            defer wg.Done()
            data := []byte(&quot;ABCDEFGHIJ&quot;)
            for i := 0; i &lt; 10; i = i + 2 {
                    &lt;-numdone
                    fmt.Printf(&quot;%c&quot;, data[i])
                    fmt.Printf(&quot;%c&quot;, data[i+1])
            }
    }()

    wg.Add(1)
    go func() {
            defer wg.Done()
            for i := 1; i &lt; 11; i = i + 2 {
                    fmt.Printf(&quot;%d&quot;, i)
                    fmt.Printf(&quot;%d&quot;, i+1)
                    numdone &lt;- i
            }
    }()

    wg.Wait()
}

答案1

得分: 1

请阅读我上面的评论。要进行同步,只需使用互斥锁即可:

package main

import (
	"fmt"
	"sync"
)

var mu sync.Mutex

func main() {
	numdone := make(chan int)
	var wg sync.WaitGroup
	wg.Add(1)
	go func() {
		defer wg.Done()
		data := []byte("ABCDEFGHIJ")
		for i := 0; i < 10; i = i + 2 {
			<-numdone
			fmt.Printf("%c", data[i])
			fmt.Printf("%c", data[i+1])
			mu.Unlock()
		}
	}()

	wg.Add(1)
	go func() {
		defer wg.Done()
		for i := 1; i < 11; i = i + 2 {
			mu.Lock()
			fmt.Printf("%d", i)
			fmt.Printf("%d", i+1)
			numdone <- i
		}
	}()

	wg.Wait()
}

Playground: https://play.golang.org/p/71Dv0iKTy_

英文:

Please read my comment above. To synchronize, just use a mutex and it'll work:

package main

import (
	&quot;fmt&quot;
	&quot;sync&quot;
)

var mu sync.Mutex

func main() {
	numdone := make(chan int)
	var wg sync.WaitGroup
	wg.Add(1)
	go func() {
		defer wg.Done()
		data := []byte(&quot;ABCDEFGHIJ&quot;)
		for i := 0; i &lt; 10; i = i + 2 {
			&lt;-numdone
			fmt.Printf(&quot;%c&quot;, data[i])
			fmt.Printf(&quot;%c&quot;, data[i+1])
			mu.Unlock()
		}
	}()

	wg.Add(1)
	go func() {
		defer wg.Done()
		for i := 1; i &lt; 11; i = i + 2 {
			mu.Lock()
			fmt.Printf(&quot;%d&quot;, i)
			fmt.Printf(&quot;%d&quot;, i+1)
			numdone &lt;- i
		}
	}()

	wg.Wait()
}

Playground : https://play.golang.org/p/71Dv0iKTy_

huangapple
  • 本文由 发表于 2017年9月6日 10:42:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/46065978.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定