1.chan\u6570\u636e\u7ed3\u6784<\/h1> \n
<\/p> \n
<\/p> \n
<\/p> \n
<\/p> \n
\u4e00\u4e2achannel\u53ea\u80fd\u4f20\u9012\u4e00\u79cd\u7c7b\u578b\u7684\u503c\uff0c\u7c7b\u578b\u4fe1\u606f\u5b58\u50a8\u5728hchan\u6570\u636e\u7ed3\u6784\u4e2d\u3002<\/p> \n
- \n
- elemtype\u4ee3\u8868\u7c7b\u578b\uff0c\u7528\u4e8e\u6570\u636e\u4f20\u9012\u8fc7\u7a0b\u4e2d\u7684\u8d4b\u503c\uff1b<\/li> \n
- elemsize\u4ee3\u8868\u7c7b\u578b\u5927\u5c0f\uff0c\u7528\u4e8e\u5728buf\u4e2d\u5b9a\u4f4d\u5143\u7d20\u4f4d\u7f6e\u3002<\/li> \n <\/ul> \n
\u4e00\u4e2achannel\u540c\u65f6\u4ec5\u5141\u8bb8\u88ab\u4e00\u4e2agoroutine\u8bfb\u5199\uff0c\u4e3a\u7b80\u5355\u8d77\u89c1\uff0c\u672c\u7ae0\u540e\u7eed\u90e8\u5206\u8bf4\u660e\u8bfb\u5199\u8fc7\u7a0b\u65f6\u4e0d\u518d\u6d89\u53ca\u52a0\u9501\u548c\u89e3\u9501\u3002<\/p> \n
2.\u521b\u5efaChan<\/h1> \n
\u521b\u5efachannel\u7684\u8fc7\u7a0b\u5b9e\u9645\u4e0a\u662f\u521d\u59cb\u5316hchan\u7ed3\u6784\u3002\u5176\u4e2d\u7c7b\u578b\u4fe1\u606f\u548c\u7f13\u51b2\u533a\u957f\u5ea6\u7531make\u8bed\u53e5\u4f20\u5165\uff0cbuf\u7684\u5927\u5c0f\u5219\u4e0e\u5143\u7d20\u5927\u5c0f\u548c\u7f13\u51b2\u533a\u957f\u5ea6\u5171\u540c\u51b3\u5b9a\u3002<\/p> \n
makeChan\u6e90\u7801\u5982\u4e0b\uff1a<\/p> \n
func makechan(t *chantype, size int) *hchan {<\/span>
elem := t.elem<\/span>
\/\/ compiler checks this but be safe.<\/span>
if elem.size >= 1<<16 {<\/span>
throw("makechan: invalid channel element type")<\/span>
}<\/span>
if hchanSize%maxAlign != 0 || elem.align > maxAlign {<\/span>
throw("makechan: bad alignment")<\/span>
}<\/span>
mem, overflow := math.MulUintptr(elem.size, uintptr(size))<\/span>
if overflow || mem > maxAlloc-hchanSize || size < 0 {<\/span>
panic(plainError("makechan: size out of range"))<\/span>
}<\/span>
\/\/ Hchan does not contain pointers interesting for GC when elements stored in buf do not contain pointers.<\/span>
\/\/ buf points into the same allocation, elemtype is persistent.<\/span>
\/\/ SudoG's are referenced from their owning thread so they can't be collected.<\/span>
\/\/ TODO(dvyukov,rlh): Rethink when collector can move allocated objects.<\/span>
var c *hchan<\/span>
switch {<\/span>
\/\/ no buffer \u7684\u573a\u666f\uff0c\u8fd9\u79cd channel \u53ef\u4ee5\u770b\u6210 pipe\uff1b<\/span>
case mem == 0:<\/span>
\/\/ Queue or element size is zero.<\/span>
c = (*hchan)(mallocgc(hchanSize, nil, true))<\/span>
\/\/ Race detector uses this location for synchronization.<\/span>
c.buf = c.raceaddr()<\/span>
case elem.ptrdata == 0:\/\/ channel \u5143\u7d20\u4e0d\u542b\u6307\u9488\u7684\u573a\u666f\uff0c\u90a3\u4e48\u662f\u5206\u914d\u51fa\u4e00\u4e2a\u5927\u5185\u5b58\u5757\uff1b<\/span>
\/\/ Elements do not contain pointers.<\/span>
\/\/ Allocate hchan and buf in one call.<\/span>
c = (*hchan)(mallocgc(hchanSize+mem, nil, true))<\/span>
c.buf = add(unsafe.Pointer(c), hchanSize)<\/span>
default:\/\/ \u9ed8\u8ba4\u573a\u666f\uff0chchan \u7ed3\u6784\u4f53\u548c buffer \u5185\u5b58\u5757\u5355\u72ec\u5206\u914d\uff1b<\/span>
\/\/ Elements contain pointers.<\/span>
c = new(hchan)<\/span>
c.buf = mallocgc(mem, elem, true)<\/span>
}<\/span>
\/\/ channel \u5143\u7d20\u5927\u5c0f\uff0c\u5982\u679c\u662f int\uff0c\u90a3\u4e48\u5c31\u662f 8 \u5b57\u8282\uff1b<\/span>
c.elemsize = uint16(elem.size)<\/span>
\/\/ \u5143\u7d20\u7c7b\u578b\uff0c\u8fd9\u6837\u5c31\u77e5\u9053 channel \u91cc\u9762\u6bcf\u4e2a\u5143\u7d20\u7a76\u7adf\u662f\u5565<\/span>
c.elemtype = elem<\/span>
c.dataqsiz = uint(size)<\/span>
lockInit(&c.lock, lockRankHchan)<\/span>
if debugChan {<\/span>
print("makechan: chan=", c, "; elemsize=", elem.size, "; dataqsiz=", size, "\\n")<\/span>
}<\/span>
return c<\/span>
}
makeChan\u53ea\u4f1a\u521d\u59cb\u5316\u56db\u4e2a\u53c2\u6570\uff1abuf,elemsize,elemtype,dataqsize
channel\u53d1\u9001\u7684\u4ee3\u7801\u89e3\u6790\u5982\u4e0b\uff1a<\/span><\/pre> \n
<\/p> \n
<\/pre> \n
<\/p> \n
<\/p> \n
<\/p> \n
<\/p> \n
<\/p> \n
channel\u63a5\u6536\u6570\u636e\u7684\u4ee3\u7801\u5982\u4e0b\uff1a<\/p> \n
<\/p> \n
<\/p> \n
<\/p> \n
<\/p> \n
<\/p> \n
<\/p> \n
<\/p> \n
<\/p> \n
<\/p> \n
<\/p> \n
<\/p> \n
<\/span><\/pre> \n<\/div>","orderid":"0","title":"Go chan\u89e3\u6790","smalltitle":"","mid":"0","fname":"GO","special_id":"0","bak_id":"0","info":"0","hits":"45","pages":"1","comments":"0","posttime":"2023-07-23 13:33:31","list":"1690090411","username":"admin","author":"","copyfrom":"","copyfromurl":"","titlecolor":"","fonttype":"0","titleicon":"0","picurl":"https:\/\/www.cppentry.com\/upload_files\/","ispic":"0","yz":"1","yzer":"","yztime":"0","levels":"0","levelstime":"0","keywords":"chan<\/A> \u89e3\u6790<\/A>","jumpurl":"","iframeurl":"","style":"","template":"a:3:{s:4:\"head\";s:0:\"\";s:4:\"foot\";s:0:\"\";s:8:\"bencandy\";s:0:\"\";}","target":"0","ip":"119.59.235.169","lastfid":"0","money":"0","buyuser":"","passwd":"","allowdown":"","allowview":"","editer":"","edittime":"0","begintime":"0","endtime":"0","description":"Go chan\u89e3\u6790","lastview":"1694893364","digg_num":"0","digg_time":"0","forbidcomment":"0","ifvote":"0","heart":"","htmlname":"","city_id":"0"},"page":"1"}