FAQ
package main

import (
"fmt"
"strconv"
"strings"
"time"
"runtime"
)

type hub struct {
connections map[string]*connection

broadcast chan string
}

type connection struct {
t int64
send chan string
}

var h = hub{
broadcast: make(chan string, 100),

connections: make(map[string]*connection),
}

func run() {
fmt.Println("run")
for {
select {

case m , ok:= <-h.broadcast:
if !ok {
fmt.Println(ok)
} else {
fmt.Println("broadcast")
s := strings.Split(m, "|")
i := s[1]
c := h.connections[i]
c.send <- s[0]
}
//default:
//fmt.Println("default")
}
}
}

func f1(i int) {
         fmt.Println("begin:", i)
s := strconv.Itoa(i)
t := time.Now().Unix()
c := connection{t, make(chan string, 256)}
h.connections[s] = &c
m := "hi" + "|" + s

h.broadcast <- m

fmt.Println("send", i, m)

select {
case message, ok := <-c.send:
if !ok {
fmt.Println("error:", ok)
} else {
fmt.Println("read", i, message)
}
time.Sleep(10 * time.Millisecond)

}
}

func f(i int) {
f1(i)
}

func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
  go run()

//time.Sleep(500 * time.Millisecond)

for i := 0; i < 10; i++ {
go f(i)
}

for{
}
}



--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • 小菜 at Nov 18, 2014 at 1:45 am
    output:


    begin: 1
    send 1 hi|1
    begin: 5
    send 5 hi|5
    begin: 3
    send 3 hi|3
    begin: 0
    send 0 hi|0
    begin: 6
    send 6 hi|6
    begin: 9





    在 2014年11月18日星期二UTC+8上午9时44分39秒,小菜写道:
    package main

    import (
    "fmt"
    "strconv"
    "strings"
    "time"
    "runtime"
    )

    type hub struct {
    connections map[string]*connection

    broadcast chan string
    }

    type connection struct {
    t int64
    send chan string
    }

    var h = hub{
    broadcast: make(chan string, 100),

    connections: make(map[string]*connection),
    }

    func run() {
    fmt.Println("run")
    for {
    select {

    case m , ok:= <-h.broadcast:
    if !ok {
    fmt.Println(ok)
    } else {
    fmt.Println("broadcast")
    s := strings.Split(m, "|")
    i := s[1]
    c := h.connections[i]
    c.send <- s[0]
    }
    //default:
    //fmt.Println("default")
    }
    }
    }

    func f1(i int) {
    fmt.Println("begin:", i)
    s := strconv.Itoa(i)
    t := time.Now().Unix()
    c := connection{t, make(chan string, 256)}
    h.connections[s] = &c
    m := "hi" + "|" + s

    h.broadcast <- m

    fmt.Println("send", i, m)

    select {
    case message, ok := <-c.send:
    if !ok {
    fmt.Println("error:", ok)
    } else {
    fmt.Println("read", i, message)
    }
    time.Sleep(10 * time.Millisecond)

    }
    }

    func f(i int) {
    f1(i)
    }

    func main() {
    runtime.GOMAXPROCS(runtime.NumCPU())
    go run()

    //time.Sleep(500 * time.Millisecond)

    for i := 0; i < 10; i++ {
    go f(i)
    }

    for{
    }
    }


    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Dave Cheney at Nov 18, 2014 at 1:48 am
    Please run your program under the race detector. I can see at least one
    data race on the h.connections map
    On Tuesday, 18 November 2014 12:44:39 UTC+11, 小菜 wrote:

    package main

    import (
    "fmt"
    "strconv"
    "strings"
    "time"
    "runtime"
    )

    type hub struct {
    connections map[string]*connection

    broadcast chan string
    }

    type connection struct {
    t int64
    send chan string
    }

    var h = hub{
    broadcast: make(chan string, 100),

    connections: make(map[string]*connection),
    }

    func run() {
    fmt.Println("run")
    for {
    select {

    case m , ok:= <-h.broadcast:
    if !ok {
    fmt.Println(ok)
    } else {
    fmt.Println("broadcast")
    s := strings.Split(m, "|")
    i := s[1]
    c := h.connections[i]
    c.send <- s[0]
    }
    //default:
    //fmt.Println("default")
    }
    }
    }

    func f1(i int) {
    fmt.Println("begin:", i)
    s := strconv.Itoa(i)
    t := time.Now().Unix()
    c := connection{t, make(chan string, 256)}
    h.connections[s] = &c
    m := "hi" + "|" + s

    h.broadcast <- m

    fmt.Println("send", i, m)

    select {
    case message, ok := <-c.send:
    if !ok {
    fmt.Println("error:", ok)
    } else {
    fmt.Println("read", i, message)
    }
    time.Sleep(10 * time.Millisecond)

    }
    }

    func f(i int) {
    f1(i)
    }

    func main() {
    runtime.GOMAXPROCS(runtime.NumCPU())
    Don't do this, it isn't helping

    go run()

    //time.Sleep(500 * time.Millisecond)

    for i := 0; i < 10; i++ {
    go f(i)
    }

    for{
    }
    Don't spin in Go programs. If you want to block the goroutine, use

        select {}

    }


    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • 小菜 at Nov 18, 2014 at 2:08 am
    thanks,

    I replace the main function with code below:

    func main() {
    runtime.GOMAXPROCS(runtime.NumCPU())
      go run()

    //time.Sleep(500 * time.Millisecond)

    for i := 0; i < 10; i++ {
    wg.Add(1)
    go f(i)
    }

    wg.Wait()
    }


    And it works fine。 But I don't quite understand the meaning of “Please
    run your program under the race detector. I can see at least one data race
    on the h.connections map”,

    This use of h.connections map will be thread safe problem? There is a
    better way?

    I this way,looks like no problem, is running normally.


    在 2014年11月18日星期二UTC+8上午9时48分31秒,Dave Cheney写道:
    Please run your program under the race detector. I can see at least one
    data race on the h.connections map
    On Tuesday, 18 November 2014 12:44:39 UTC+11, 小菜 wrote:

    package main

    import (
    "fmt"
    "strconv"
    "strings"
    "time"
    "runtime"
    )

    type hub struct {
    connections map[string]*connection

    broadcast chan string
    }

    type connection struct {
    t int64
    send chan string
    }

    var h = hub{
    broadcast: make(chan string, 100),

    connections: make(map[string]*connection),
    }

    func run() {
    fmt.Println("run")
    for {
    select {

    case m , ok:= <-h.broadcast:
    if !ok {
    fmt.Println(ok)
    } else {
    fmt.Println("broadcast")
    s := strings.Split(m, "|")
    i := s[1]
    c := h.connections[i]
    c.send <- s[0]
    }
    //default:
    //fmt.Println("default")
    }
    }
    }

    func f1(i int) {
    fmt.Println("begin:", i)
    s := strconv.Itoa(i)
    t := time.Now().Unix()
    c := connection{t, make(chan string, 256)}
    h.connections[s] = &c
    m := "hi" + "|" + s

    h.broadcast <- m

    fmt.Println("send", i, m)

    select {
    case message, ok := <-c.send:
    if !ok {
    fmt.Println("error:", ok)
    } else {
    fmt.Println("read", i, message)
    }
    time.Sleep(10 * time.Millisecond)

    }
    }

    func f(i int) {
    f1(i)
    }

    func main() {
    runtime.GOMAXPROCS(runtime.NumCPU())
    Don't do this, it isn't helping

    go run()

    //time.Sleep(500 * time.Millisecond)

    for i := 0; i < 10; i++ {
    go f(i)
    }

    for{
    }
    Don't spin in Go programs. If you want to block the goroutine, use

    select {}

    }


    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Francisco Dalla Rosa Soares at Nov 18, 2014 at 2:11 am
    check this link out:

    http://golang.org/doc/articles/race_detector.html

    To help diagnose such bugs, Go includes a built-in data race detector.
    To use it, add the -race flag to the go command:

    $ go test -race mypkg // to test the package
    $ go run -race mysrc.go // to run the source file
    $ go build -race mycmd // to build the command
    $ go install -race mypkg // to install the package
    On Tue, Nov 18, 2014 at 11:08 AM, 小菜 wrote:
    thanks,

    I replace the main function with code below:

    func main() {
    runtime.GOMAXPROCS(runtime.NumCPU())
    go run()

    //time.Sleep(500 * time.Millisecond)

    for i := 0; i < 10; i++ {
    wg.Add(1)
    go f(i)
    }

    wg.Wait()
    }


    And it works fine。 But I don't quite understand the meaning of “Please run
    your program under the race detector. I can see at least one data race on
    the h.connections map”,

    This use of h.connections map will be thread safe problem? There is a better
    way?

    I this way,looks like no problem, is running normally.



    在 2014年11月18日星期二UTC+8上午9时48分31秒,Dave Cheney写道:
    Please run your program under the race detector. I can see at least one
    data race on the h.connections map
    On Tuesday, 18 November 2014 12:44:39 UTC+11, 小菜 wrote:

    package main

    import (
    "fmt"
    "strconv"
    "strings"
    "time"
    "runtime"
    )

    type hub struct {
    connections map[string]*connection

    broadcast chan string
    }

    type connection struct {
    t int64
    send chan string
    }

    var h = hub{
    broadcast: make(chan string, 100),

    connections: make(map[string]*connection),
    }

    func run() {
    fmt.Println("run")
    for {
    select {

    case m , ok:= <-h.broadcast:
    if !ok {
    fmt.Println(ok)
    } else {
    fmt.Println("broadcast")
    s := strings.Split(m, "|")
    i := s[1]
    c := h.connections[i]
    c.send <- s[0]
    }
    //default:
    //fmt.Println("default")
    }
    }
    }

    func f1(i int) {
    fmt.Println("begin:", i)
    s := strconv.Itoa(i)
    t := time.Now().Unix()
    c := connection{t, make(chan string, 256)}
    h.connections[s] = &c
    m := "hi" + "|" + s

    h.broadcast <- m

    fmt.Println("send", i, m)

    select {
    case message, ok := <-c.send:
    if !ok {
    fmt.Println("error:", ok)
    } else {
    fmt.Println("read", i, message)
    }
    time.Sleep(10 * time.Millisecond)

    }
    }

    func f(i int) {
    f1(i)
    }

    func main() {
    runtime.GOMAXPROCS(runtime.NumCPU())

    Don't do this, it isn't helping
    go run()

    //time.Sleep(500 * time.Millisecond)

    for i := 0; i < 10; i++ {
    go f(i)
    }

    for{
    }

    Don't spin in Go programs. If you want to block the goroutine, use

    select {}
    }

    --
    You received this message because you are subscribed to the Google Groups
    "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an
    email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • 叶新伟 at Nov 18, 2014 at 2:27 am
    thanks everyone, I understand!

    2014-11-18 10:11 GMT+08:00 Francisco Dalla Rosa Soares <dallarosa@gmail.com>
    :
    check this link out:

    http://golang.org/doc/articles/race_detector.html

    To help diagnose such bugs, Go includes a built-in data race detector.
    To use it, add the -race flag to the go command:

    $ go test -race mypkg // to test the package
    $ go run -race mysrc.go // to run the source file
    $ go build -race mycmd // to build the command
    $ go install -race mypkg // to install the package
    On Tue, Nov 18, 2014 at 11:08 AM, 小菜 wrote:
    thanks,

    I replace the main function with code below:

    func main() {
    runtime.GOMAXPROCS(runtime.NumCPU())
    go run()

    //time.Sleep(500 * time.Millisecond)

    for i := 0; i < 10; i++ {
    wg.Add(1)
    go f(i)
    }

    wg.Wait()
    }


    And it works fine。 But I don't quite understand the meaning of “Please run
    your program under the race detector. I can see at least one data race on
    the h.connections map”,

    This use of h.connections map will be thread safe problem? There is a better
    way?

    I this way,looks like no problem, is running normally.



    在 2014年11月18日星期二UTC+8上午9时48分31秒,Dave Cheney写道:
    Please run your program under the race detector. I can see at least one
    data race on the h.connections map
    On Tuesday, 18 November 2014 12:44:39 UTC+11, 小菜 wrote:

    package main

    import (
    "fmt"
    "strconv"
    "strings"
    "time"
    "runtime"
    )

    type hub struct {
    connections map[string]*connection

    broadcast chan string
    }

    type connection struct {
    t int64
    send chan string
    }

    var h = hub{
    broadcast: make(chan string, 100),

    connections: make(map[string]*connection),
    }

    func run() {
    fmt.Println("run")
    for {
    select {

    case m , ok:= <-h.broadcast:
    if !ok {
    fmt.Println(ok)
    } else {
    fmt.Println("broadcast")
    s := strings.Split(m, "|")
    i := s[1]
    c := h.connections[i]
    c.send <- s[0]
    }
    //default:
    //fmt.Println("default")
    }
    }
    }

    func f1(i int) {
    fmt.Println("begin:", i)
    s := strconv.Itoa(i)
    t := time.Now().Unix()
    c := connection{t, make(chan string, 256)}
    h.connections[s] = &c
    m := "hi" + "|" + s

    h.broadcast <- m

    fmt.Println("send", i, m)

    select {
    case message, ok := <-c.send:
    if !ok {
    fmt.Println("error:", ok)
    } else {
    fmt.Println("read", i, message)
    }
    time.Sleep(10 * time.Millisecond)

    }
    }

    func f(i int) {
    f1(i)
    }

    func main() {
    runtime.GOMAXPROCS(runtime.NumCPU())

    Don't do this, it isn't helping
    go run()

    //time.Sleep(500 * time.Millisecond)

    for i := 0; i < 10; i++ {
    go f(i)
    }

    for{
    }

    Don't spin in Go programs. If you want to block the goroutine, use

    select {}
    }

    --
    You received this message because you are subscribed to the Google Groups
    "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an
    email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • 小菜 at Nov 18, 2014 at 2:31 am
    Efficiency with buffer channel and not buffered channel difference whether
    there was much difference? Under what circumstances I should use buffered
      channel?



    在 2014年11月18日星期二UTC+8上午9时44分39秒,小菜写道:
    package main

    import (
    "fmt"
    "strconv"
    "strings"
    "time"
    "runtime"
    )

    type hub struct {
    connections map[string]*connection

    broadcast chan string
    }

    type connection struct {
    t int64
    send chan string
    }

    var h = hub{
    broadcast: make(chan string, 100),

    connections: make(map[string]*connection),
    }

    func run() {
    fmt.Println("run")
    for {
    select {

    case m , ok:= <-h.broadcast:
    if !ok {
    fmt.Println(ok)
    } else {
    fmt.Println("broadcast")
    s := strings.Split(m, "|")
    i := s[1]
    c := h.connections[i]
    c.send <- s[0]
    }
    //default:
    //fmt.Println("default")
    }
    }
    }

    func f1(i int) {
    fmt.Println("begin:", i)
    s := strconv.Itoa(i)
    t := time.Now().Unix()
    c := connection{t, make(chan string, 256)}
    h.connections[s] = &c
    m := "hi" + "|" + s

    h.broadcast <- m

    fmt.Println("send", i, m)

    select {
    case message, ok := <-c.send:
    if !ok {
    fmt.Println("error:", ok)
    } else {
    fmt.Println("read", i, message)
    }
    time.Sleep(10 * time.Millisecond)

    }
    }

    func f(i int) {
    f1(i)
    }

    func main() {
    runtime.GOMAXPROCS(runtime.NumCPU())
    go run()

    //time.Sleep(500 * time.Millisecond)

    for i := 0; i < 10; i++ {
    go f(i)
    }

    for{
    }
    }


    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedNov 18, '14 at 1:44a
activeNov 18, '14 at 2:31a
posts7
users3
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase