A variety of permanently blocked poses in the Golang

Source: Internet
Author: User
Tags disqus
This is a creation in Article, where the information may have evolved or changed.

A variety of permanently blocked poses in the Golang

The current design of the go runtime assumes that the programmer is responsible for detecting when to terminate a goroutine and when to terminate the program. Can be called by the OS. Exit or return from the main () function to terminate the program in the normal manner. And sometimes what we need is to make the program block in this line.

Use Sync. Waitgroup

Wait until Waitgroup equals 0.

package mainimport "sync"func main() {    var wg sync.WaitGroup    wg.Add(1)    wg.Wait()}

Empty Select

select{} is a select without any case, it will always block

package mainfunc main() {    select{}}

Dead loop

Although it can block, it will occupy a CPU 100%. Do not recommend using

package mainfunc main() {    for {}}

Use Sync. Mutex

A lock that has been locked, locked again will be blocked, this is not recommended to use

package mainimport "sync"func main() {    var m sync.Mutex    m.Lock()    m.Lock()}

Os. Signal

System semaphore, which is also a channel in go, blocking until a specific message is received

package mainimport (    "os"    "syscall"    "os/signal")func main() {    sig := make(chan os.Signal, 2)    signal.Notify(sig, syscall.SIGTERM, syscall.SIGINT)    <-sig}

Empty channel or nil channel

The channel is blocked until the message is received, and the nil channel is blocked forever.

package mainfunc main() {    c := make(chan struct{})    <-c}
package mainfunc main() {    var c chan struct{} //nil channel    <-c}

Summarize

Note that most of the code written above cannot be run directly, it will be panic, "all Goroutines is asleep-deadlock!", because go runtime will check all your goroutine are stuck, no one to execute. You can add one or more goroutine to your own business logic in front of the blocking code, so you won't be deadlock.

Reference

http://pliutau.com/different-ways-to-block-go-runtime-forever/

    • ←previous Post
Please enable JavaScript to view the comments powered by Disqus. Comments Powered by Disqus
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.