当前位置: 首页>>代码示例>>Golang>>正文


Golang Grid.Subscribe方法代码示例

本文整理汇总了Golang中github.com/dnesting/alife/goalife/grid2d.Grid.Subscribe方法的典型用法代码示例。如果您正苦于以下问题:Golang Grid.Subscribe方法的具体用法?Golang Grid.Subscribe怎么用?Golang Grid.Subscribe使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在github.com/dnesting/alife/goalife/grid2d.Grid的用法示例。


在下文中一共展示了Grid.Subscribe方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: startUpdateTracker

func startUpdateTracker(g grid2d.Grid, numUpdates *int64) {
	ch := make(chan []grid2d.Update, 0)
	go func() {
		for updates := range ch {
			atomic.AddInt64(numUpdates, int64(len(updates)))
		}
	}()
	g.Subscribe(ch)
}
开发者ID:dnesting,项目名称:alife,代码行数:9,代码来源:main.go

示例2: startAndMaintainOrgs

func startAndMaintainOrgs(g grid2d.Grid) {
	// Obtain an initial count before we start anything executing.
	mCount := maintain.Count(g, isOrg)

	ch := make(chan []grid2d.Update, 0)
	g.Subscribe(ch)

	// Start all organisms currently existing in the Grid.  We do this *after*
	// subscribing ch so that we don't end up with a wrong count if any organisms
	// divide or die.
	cpu1.StartAll(g)

	go maintain.Maintain(ch, isOrg, func() { startOrg(g) }, minOrgs, mCount)
}
开发者ID:dnesting,项目名称:alife,代码行数:14,代码来源:main.go

示例3: startPrintLoop

func startPrintLoop(g grid2d.Grid, cns *census.DirCensus, cond *sync.Cond, numUpdates *int64, clearScreen bool) {
	// We want to use chanbuf.Tick to ensure renders occur at specific intervals regardless
	// of the rate at which updates arrive.  To prevent the notification channel from backing up
	// and causing deadlock, we buffer using a chanbuf.Trigger (since we don't care about the
	// update messages themselves).  We could have used a time.Tick instead, but we'd need to
	// do something special to ensure this loop exits when the notifications stop.

	// grid notifier -> updateCh -> trigger -> tick -> print world
	//   grid notifier first dispatches an update the moment it occurs
	//   trigger consumes the event and flags that an update occurred
	//   tick fires every freq, halting when trigger reports it's closed
	//   printLoop renders the grid every time tick fires

	updateCh := make(chan []grid2d.Update, 0)
	trigger := chanbuf.Trigger()
	freq := time.Duration(1000000.0/printRate) * time.Microsecond
	go chanbuf.Feed(trigger, grid2d.NotifyToInterface(updateCh))
	tickCh := chanbuf.Tick(trigger, freq, true)
	g.Subscribe(updateCh)

	go printLoop(grid2d.NotifyFromInterface(tickCh), g, cns, cond, numUpdates, clearScreen)
}
开发者ID:dnesting,项目名称:alife,代码行数:22,代码来源:main.go

示例4: startCensus

func startCensus(g grid2d.Grid) *census.DirCensus {
	// Create a new Census that writes to /tmp/census when a population grows to 40.
	cns, err := census.NewDirCensus("/tmp/census", func(p census.Population) bool { return p.Count > 40 })
	if err != nil {
		fmt.Printf("Error creating census: %v\n", err)
		os.Exit(1)
	}

	// Use human times.
	timeNow := func(interface{}) interface{} { return time.Now() }

	ch := make(chan []grid2d.Update, 0)
	g.Subscribe(ch)

	// Populate the Census with what's already in the world (perhaps restored from an autosave).
	// Assumes nothing in the world is changing yet.
	grid2d.ScanForCensus(cns, g, timeNow, orgHash)

	// Start monitoring for changes
	go grid2d.WatchForCensus(cns, ch, timeNow, orgHash)

	return cns
}
开发者ID:dnesting,项目名称:alife,代码行数:23,代码来源:main.go


注:本文中的github.com/dnesting/alife/goalife/grid2d.Grid.Subscribe方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。