An example of a semaphore and a timer (Golang)

Source: Internet
Author: User

The program can be used to perform some tasks on a regular basis, and it can also do the appropriate warning and cleanup when the force is interrupted by the semaphore processing.

Package main//semaphore and timer//author:xiong Chuan liang//date:2015-2-25import "FMT" import "OS" import "os/signal" import "Time" Func Main () {sigs: = Make (chan os. Signal, 1) Done: = Make (chan bool, 1) Signal. Notify (sigs, OS. Interrupt, OS. Kill) go func () {sig: = <-sigsswitch sig {case OS. Interrupt:fmt. Println ("Signal:interrupt") case OS. Kill:fmt. Println ("Signal:kill") default:fmt. Println ("Signal:others")}done <-True} () Fmt. PRINTLN ("Awaiting signal")//main () .... Go jobticker (done) <-doneclose. Exit () fmt. Println ("exiting")}func Jobticker (done <-chan bool) {ticker: = time. Newticker (time. Second) defer ticker. Stop () for {select {case <-done:returncase <-ticker. C:fmt. Println ("Job .....")}}}


If you press CTRL-C, the result is as follows:

Awaiting signaljob......job......signal:interruptinterruptexiting


Go by Example:signals:https://gobyexample.com/signals


MAIL: [Email protected]

blog:http://blog.csdn.net/xcl168



An example of a semaphore and a timer (Golang)

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.