Share Memory By Communicating 一等公民
Share Memory By Communicating - The Go Programming Language https://golang.google.cn/doc/codewalk/sharemem/
One suggestion (made by Rob Pike) for concurrent programming is don't (let computations) communicate by sharing memory, (let them) share memory by communicating (through channels). (We can view each computation as a goroutine in Go programming.)
Communicating by sharing memory and sharing memory by communicating are two programming manners in concurrent programming. When goroutines communicate by sharing memory, we use traditional concurrency synchronization techniques, such as mutex locks, to protect the shared memory to prevent data races. We can use channels to implement sharing memory by communicating.
Go provides a unique concurrency synchronization technique, channel. Channels make goroutines share memory by communicating. We can view a channel as an internal FIFO (first in, first out) queue within a program. Some goroutines send values to the queue (the channel) and some other goroutines receive values from the queue.
Along with transferring values (through channels), the ownership of some values may also be transferred between goroutines. When a goroutine sends a value to a channel, we can view the goroutine releases the ownership of some values. When a goroutine receives a value from a channel, we can view the goroutine acquires the ownerships of some values.
Surely, there may be also not any ownership transferred along with channel communications.
The values (whose ownerships are transferred) are often referenced (but are not required to be referenced) by the transferred value. Please note, here, when we talk about ownership, we mean the ownership from the logic view. Unlike Rust language, Go doesn't ensure value ownership from the syntax level. Go channels can help programmers write data races free code easily, but Go channels can't prevent programmers from writing bad concurrent code from the syntax level.
Although Go also supports traditional concurrency synchronization techniques. only channel is first-class citizen in Go. Channel is one kind of types in Go, so we can use channels without importing any packages. On the other hand, those traditional concurrency synchronization techniques are provided in the sync and sync/atomic standard packages.
Honestly, each concurrency synchronization technique has its own best use scenarios. But channel has a wider application range and has more variety in using. One problem of channels is, the experience of programming with channels is so enjoyable and fun that programmers often even prefer to use channels for the scenarios which channels are not best for.
https://go101.org/article/channel.html
Share Memory By Communicating 一等公民的更多相关文章
- Share Memory By Communicating
Share Memory By Communicating - The Go Programming Language https://golang.google.cn/doc/codewalk/sh ...
- scala 学习笔记(07) 一等公民的函数
在scala中一切皆对象,一切皆函数,函数跟Int,String.Class等其它类型是处于同等的地位,换句话说,使用函数跟使用普通的类型一样,没什么区别,因此: 1.函数可以赋值给变量,可以当参数传 ...
- javascript世界一等公民—函数
简介 在很多传统语言(C/C++/Java/C#等)中,函数都是作为一个二等公民存在,你只能用语言的关键字声明一个函数然后调用它,如果需要把函数作为参数传给另一个函数,或是赋值给一个本地变量,又或是作 ...
- Function Programming - First Class(一等公民function)
引用外界一等公民的定义:"在JavaScript世界中函数却是一等公民,它不仅拥有一切传统函数的使用方式(声明和调用),而且可以做到像简单值一样赋值.传参.返回,这样的函数也称之为第一级函数 ...
- JS中的一等公民:函数
在JavaScript中,函数可以 作为值赋给一个变量 作为参数传递给另一个函数 作为另一个函数的返回值 所以我们说JavaScript的函数是“一等公民”. 赋值: var foo = functi ...
- JavaScript世界的一等公民—— 函数
简介 在很多传统语言(C/C++/Java/C#等)中,函数都是作为一个二等公民存在,你只能用语言的关键字声明一个函数然后调用它,如果需要把函数作为参数传给另一个函数,或是赋值给一个本地变量,又或是作 ...
- JavaScript世界的一等公民 - 函数
简介 在很多传统语言(C/C++/Java/C#等)中,函数都是作为一个二等公民存在,你只能用语言的关键字声明一个函数然后调用它,如果需要把函数作为参数传给另一个函数,或是赋值给一个本地变量,又或是作 ...
- javascript语言中的一等公民-函数
简介 在很多传统语言(C/C++/Java/C#等)中,函数都是作为一个二等公民存在,你只能用语言的关键字声明一个函数然后调用它,如果需要把函数作为参数传给另一个函数,或是赋值给一个本地变量,又或是作 ...
- JavaScript深入浅出第2课:函数是一等公民是什么意思呢?
摘要: 听起来很炫酷的一等公民是啥? <JavaScript深入浅出>系列: JavaScript深入浅出第1课:箭头函数中的this究竟是什么鬼? JavaScript深入浅出第2课:函 ...
随机推荐
- 面向切面@Aspect
package com.imooc.demo.filter; import org.springframework.core.Ordered; import org.springframework.c ...
- kickstart+pxe部署
------------恢复内容开始------------ kickstart 通过网络安装系统 ----pxe kickstart,cobbler pex 预启动执行环境 通过网络接口启动计算机, ...
- java 反射随记
记录一下有关 Class 对象的相关方法: 1.获取 Class 对象的三个方法: ⑴ 使用 Class.forName("全限定类名") ,参数是该类的全限定类名,可拓展性强: ...
- 【探索之路】机器人篇(4)-根据3D文件来优化自己的机器人模型
此章节不是必须做的!!!! 因为我已经用solidworks画了机器人的3D模型,那我就直接导入已经画好的三维模型. 如果大家没有画也是可以直接使用上一章节我们已经构建的机器人模型.我这里只是一个对显 ...
- JDBC(五)—— 批量插入数据
批量插入数据 @Test public void testInsert() throws Exception { Connection conn = null; PreparedStatement p ...
- SQL Server中模式(schema)、数据库(database)、表(table)、用户(user)之间的关系
数据库的初学者往往会对关系型数据库模式(schema).数据库(database).表(table).用户(user)之间感到迷惘,总感觉他们的关系千丝万缕,但又不知道他们的联系和区别在哪里,对一些问 ...
- VScode中配置C++运行环境
目录 VScode中配置C++运行环境 1. 哪些插件 2. 配置开始 3. 编写代码并运行 VScode中配置C++运行环境 关于安装mingw的教程,网络上已经有很多了,这里不再赘述,下面就看VS ...
- ECMAScript概述及浅谈const,let与块级作用域
ECMAScript可以看作javascript的标准规范,实际上javascript是ECMAScript的一门脚本语言,ECMAScript只提供了最基本的语言JavaScript对ECMAScr ...
- G客短信平台开发,资源短信功能使用说明
短信平台使用资源短信操作顺序 联系微信:290615413 1:登录客户端 2:点击左侧 发送短信中的,资源短信 3:资源短信申请操作 3.1:选择相应的省市 会显示资源数量. 3.2:然后输入申请 ...
- js--实现限制input输入框数字输入,实现每四位一个空格效果(银行卡号,手机号等)
前言 工作学习中经常能遇到输入框限制输入数字,并且每四位一空格的情况,比如表单中银行卡号,手机号等输入框的限制,这里介绍一下使用js具体的实现方法.不需要引用第三方ui库. 正文 1.input标签的 ...