TODO:Go语言goroutine和channel使用

TODO:Go语言goroutine和channel使用

goroutine是Go语言中的轻量级线程实现,由Go语言运行时(runtime)管理。使用的时候在函数前面加“go”这个单词作为关键词,也是与普通函数的区别了。在函数前面加go关键字就可以创建一个新的goroutine进行并发执行。

go hello()

channel是Go语言提供的goroutine间的通信方式,我们可以使用channel在两个或多个goroutine之家传递消息。channel使用的关键字是用“chan”,声明一个传递类型为int的channel:

var ch chan int

下面小编举一个例子,监听进程被关闭后触发的事件。

监听信号,使用

signal.Notify(c, os.Interrupt)

第一个参数表示接收信号的channel

第二个及后面的参数表示设置要监听的信号,如果不设置表示监听所有的信号。

完整例子如下,本例在Ubuntu14.04的服务版测试通过。

Ctrl+c的效果如图:

Kill进程的效果如图

简单的例子,希望对您有帮助。


wxgzh:ludong86

qrcode_for_gh_6bb1f39ae99c_258
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
填充下面这个程序所有出现// TODO: fill the code这个任务的地方#include <iostream> #include <cstring> #include "ourstring.h" #include "strlib.h" using namespace std; OurString::OurString(){ // TODO: fill the code } OurString::OurString(const char *str){ // TODO: fill the code } OurString::OurString(const OurString &dstr){ // TODO: fill the code } OurString::~OurString(){ // TODO: fill the code } string OurString::toString() const{ // TODO: fill the code } OurString OurString::subStr(unsigned int start, unsigned int n) const{ // TODO: fill the code } bool OurString::operator > (const OurString &dstr) const{ // TODO: fill the code } bool OurString::operator < (const OurString &dstr) const{ // TODO: fill the code } bool OurString::operator == (const OurString &dstr) const{ // TODO: fill the code } unsigned int OurString::length () const{ // TODO: fill the code } const OurString& OurString::operator = (const OurString &dstr){ // TODO: fill the code } const OurString& OurString::operator = (const char *str){ // TODO: fill the code } char& OurString::operator[](int index){ // TODO: fill the code } const OurString OurString::operator + (const OurString &dstr) const{ // TODO: fill the code } const OurString OurString::operator + (const char *str) const{ // TODO: fill the code } const OurString& OurString::operator += (const OurString &dstr){ // TODO: fill the code } const OurString& OurString::operator += (const char *str){ // TODO: fill the code } ostream & operator<<(ostream &os, const OurString &dstr){ // TODO: fill the code } istream & operator>>(istream &is, OurString &dstr){ // TODO: fill the code }
最新发布
05-29

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值