普通网友 2015-05-30 16:16
浏览 24

如何关闭正在等待I / O的goroutine

I have created two go routines sender and receiver, sender will continuous get the data from the user(keyboard) and write to stream, receiver will independently get the value from stream print it to the screen. Both are concurrent using go routine

At some point of time receiver failed and close the connection as well as exit the receiver go routine, but sender go routine which waiting for user input(i/o operation) will not be closed. How to exit all the go routines in this scenario?

Below is the piece of sample code for this scenario.

package main

import (
    "fmt"
    "time"
)

var stop bool = false

func sender() {
    str := ""
    for !stop {
        fmt.Scanf("%s", &str)
        fmt.Println("Entered :", str)
    }   
    fmt.Println("Closing sender goroutine")
}

func receiver() {
    i := 0
    for !stop {
        i++
        if i > 5 { 
            stop = true
        }
        time.Sleep(1 * time.Second)
    }   
    fmt.Println("Closing receiver goroutine")
}

func main() {
    go sender()
    go receiver()

    /* Wait for goroutines to finish */
    for !stop {
        time.Sleep(1 * time.Millisecond)
    }   
    time.Sleep(1 * time.Second)

    panic("Display stack")
}

Above code sender will wait for user input after 5 loop receiver will exit the receiver go routine. I expect when receiver close, go routine which waiting on i/o has to be closed.

Kindly help me on this question.

  • 写回答

2条回答 默认 最新

  • drby30217 2015-05-30 18:16
    关注

    To start, your code has a race around the stop variable. When there's a data race, there's no guarantee your program will behave as defined. Use channels to synchronize goroutines. This however isn't why you program continues.

    Your code is blocking on fmt.Scanf, and doesn't get to check the stop condition. Since a Read on Stdin can't be interrupted (which is happening inside fmt.Scanf), you need to check for the stop condition before calling Scanfagain. If there's no more input, but you have a pending Read on Stdin, the easiest way to handle it is to just let leave that goroutine running. There are some rather complex ways to break out of this using something known as the "self-pipe" trick, but it's generally not worth the effort, as goroutines are small and don't take many resources.

    for !stop {
        fmt.Scanf("%s", &str)
        fmt.Println("Entered :", str)
        // use a channel to detect when to exit
        select {
        case <-stop:
            return
        default:
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100