Giter Club home page Giter Club logo

Comments (7)

Momo733 avatar Momo733 commented on July 21, 2024

对于这种的解决方法,我一直有一个问题,为什么最后的goruntine会最先执行,下面是代码:

package main

import (
	"runtime"
	"sync"
	"fmt"
)

const N = 10

func main() {
	runtime.GOMAXPROCS(1)
	wg := &sync.WaitGroup{}
	wg.Add(N*2)
	for i := 0; i < N; i++ {
		m := i
		go func(m int) {
			defer wg.Done()
			fmt.Print(m)
		}(m)
		go func(m int) {
			defer wg.Done()
			runtime.Gosched()
			fmt.Printf(`%c`,'A'+m)
		}(m)
	}
	wg.Wait()
}

结果是J0123456789ABCDEFGHI J居然是最先打印的,大佬知道原因么,谢谢!

from interview-go.

lifei6671 avatar lifei6671 commented on July 21, 2024

貌似是golang的调度器并不能保证协程是顺序执行的。

from interview-go.

tlightsky avatar tlightsky commented on July 21, 2024

也贴一个我的:
https://gist.github.com/tlightsky/a67e3ee1360e79432478d0baa4a3cab6

from interview-go.

linture avatar linture commented on July 21, 2024
package main
import (
  "fmt"
)

// POINT: communicate between goroutines by channel

//使用两个 goroutine 交替打印序列,一个 goroutine 打印数字, 另外一个 goroutine 打印字母, 最终效果如下:
//12AB34CD56EF78GH910IJ1112KL1314MN1516OP1718QR1920ST2122UV2324WX2526YZ2728
func AlterPrint(){
  letter, number := make(chan bool), make(chan bool)
  letterDone := make(chan bool)
  go func() {
    i := 1
    for {
      select{
        case <-number: {
          fmt.Print(i)
          i++
          fmt.Print(i)
          i++
          letter <- true
          break
        }
        default: {
          break
        }
      }

    }
  }()

  go func(){
    i := 'A'
    for {
      if i > 'Z' {
        letterDone <- true
        return
      }
      select{
        case <-letter: {
          fmt.Print(string(i))
          i++
          fmt.Print(string(i))
          i++
          number <- true
          break
        }
        default: {
          break
        }
      }
    }
  }()
  number <- true
  <- letterDone
}

from interview-go.

dchaofei avatar dchaofei commented on July 21, 2024
	letter, number := make(chan struct{}), make(chan struct{})
	var wg sync.WaitGroup
	wg.Add(2)
	go func() {
		defer wg.Done()
		i := 1
		pr := func() {
			fmt.Print(i)
			i++
		}
		for {
			_, ok := <-number
			if !ok {
				close(letter)
				break
			}
			pr()
			pr()
			letter <- struct{}{}
		}
	}()
	go func() {
		defer wg.Done()
		str := "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
		length := len(str)

		i := 0
		pr := func() bool {
			if i == length {
				return false
			}
			fmt.Print(string(str[i]))
			i++
			return true
		}
		for {
			_, ok := <-letter
			if !ok || !pr() || !pr() {
				break
			}
			number <- struct{}{}
		}
		close(number)
	}()
	number <- struct{}{}
	wg.Wait()

from interview-go.

 avatar commented on July 21, 2024

贴一个俺的😛

func main() {
	letter, number := make(chan int, 1), make(chan int, 1)
	var wg sync.WaitGroup
	wg.Add(2)
	defer func() {
		close(letter)
		close(number)
	}()

	go func() {
		for {
			select {
			case num := <-number:
				if num >= 13 {
					wg.Done()
					return
				}
				fmt.Printf("%d%d", 2*num+1, 2*num+2)
				letter <- num
			}
		}
	}()
	go func() {
		for {
			select {
			case let := <-letter:
				fmt.Printf("%c%c", 'A'+2*let, 'A'+2*let+1)
				number <- let + 1
				if let >= 12 {
					wg.Done()
					return
				}
			}
		}
	}()
	number <- 0
	wg.Wait()
}

from interview-go.

NightmareZero avatar NightmareZero commented on July 21, 2024
package main

import (
	"fmt"
	"runtime"
	"sync"
)

const N = 28

func main() {
	runtime.GOMAXPROCS(1)
	wg := &sync.WaitGroup{}
	wg.Add(N)
	for i := 0; i < N; i++ {
		go func(m int) {
			defer wg.Done()
			fmt.Printf(`%d%d`,m+1,m+2)
			runtime.Gosched()
		}(i)
		go func(m int) {
			defer wg.Done()
			if m == 26 {
				runtime.Goexit()
			}
			fmt.Printf(`%c%c`, 'A'+m, 'A'+m+1)
		}(i)
		i = i + 1
	}
	wg.Wait()
}

总共这么一个需求犯得着在for循环里面放goroutine么,这个不是禁忌么

from interview-go.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.