官术网_书友最值得收藏!

Single goroutine halting the complete program

We know that goroutines run across multiple threads and multiple cores. So what happens when we have a panic in one of the threads? Here is an example that would let us simulate such a situation. We will create a lot of similar goroutines, whose sole purpose is to take a number and divide it by itself after subtracting 10 from the denominator. This will work fine for the majority of cases, except when the number is 10. The following code implements the described functionality:

package main 
 
import ( 
    "fmt" 
    "sync" 
) 
 
func simpleFunc(index int, wg *sync.WaitGroup) { 
    // This line should fail with Divide By Zero when index = 10 
    fmt.Println("Attempting x/(x-10) where x = ", index, " answer is : ", index/(index-10)) 
    wg.Done() 
} 
 
func main() { 
    var wg sync.WaitGroup 
    wg.Add(40) 
    for i := 0; i < 40; i += 1 { 
        go func(j int) { 
            simpleFunc(j, &wg) 
        }(i) 
    } 
 
    wg.Wait() 
}

The output of the previous code can be as follows:

Attempting x/(x-10) where x =  39  answer is :  1
Attempting x/(x-10) where x = 20 answer is : 2...
Attempting x/(x-10) where x = 37 answer is : 1
Attempting x/(x-10) where x = 11 answer is : 11
panic: runtime error: integer divide by zerogoroutine 15 [running]:main.simpleFunc(0xa, 0xc42000e280)
...
exit status 2

Essentially, a lot of goroutines were put in the runqueue, and upon being executed in random order, their outputs were printed to the console. However, as soon as the goroutine with index == 10 was executed, it raised a panic which was not handled by the function, and this resulted in the complete program halting and exiting with status code 2. This shows that even a single error or panic that hasn't been handled will halt the complete program!

However, it wouldn't make sense to crash the program because we faced a panic that we might have been able to handle graciously. Go allows us to recover from a panic with an appropriately named function called recover. Let's look at how to use recover in the previous code example:

package main 
 
import ( 
    "fmt" 
    "sync" 
) 
 
func simpleFunc(index int, wg *sync.WaitGroup) { 
    // functions with defer keyword are executed at the end of the function 
    // regardless of whether the function was executed successfully or not. 
    defer func() { 
        if r := recover(); r != nil { 
            fmt.Println("Recovered from", r) 
        } 
    }() 
 
    // We have changed the order of when wg.Done is called because 
    // we should call upon wg.Done even if the following line fails. 
    // Whether a defer function exists or not is dependent on whether it is registered 
    // before or after the failing line of code. 
    defer wg.Done() 
    // This line should fail with Divide By Zero when index = 10 
    fmt.Println("Attempting x/(x-10) where x = ", index, " answer is : ", index/(index-10)) 
} 
 
func main() { 
    var wg sync.WaitGroup 
    wg.Add(40) 
    for i := 0; i < 40; i += 1 { 
        go func(j int) { 
            simpleFunc(j, &wg) 
        }(i) 
    } 
 
    wg.Wait() 
}

The output for the preceding code can be as follows:

Attempting x/(x-10) where x =  39  answer is :  1                                     Attempting x/(x-10) where x =  14  answer is :  3                                     Recovered from runtime error: integer divide by zero                                  Attempting x/(x-10) where x =  3  answer is :  0                                      ...Attempting x/(x-10) where x =  29  answer is :  1                                     Attempting x/(x-10) where x =  9  answer is :  -9 
主站蜘蛛池模板: 高台县| 察雅县| 彰化县| 监利县| 阳朔县| 彩票| 石屏县| 神农架林区| 新宾| 日土县| 建水县| 江源县| 班玛县| 遂平县| 翁牛特旗| 温泉县| 绥宁县| 灵石县| 抚州市| 鄂温| 石家庄市| 湛江市| 康马县| 客服| 南通市| 南部县| 沿河| 高雄县| 阿拉善左旗| 独山县| 大兴区| 利辛县| 横峰县| 缙云县| 平远县| 白山市| 定襄县| 环江| 密云县| 彭阳县| 普安县|