160 likes | 364 Views
Go Language * Go-Routines *Channels. New Concepts. Do not communicate by sharing memory; instead, share memory by communicating. creating shared memory VS sharing by communication channels . Lightweight Threads ( Goroutines ). Goroutines.
E N D
New Concepts • Do not communicate by sharing memory; instead, share memory by communicating. • creating shared memory VSsharing by communication channels. • Lightweight Threads (Goroutines)
Goroutines • a Go function or method executing concurrently in the same address space as other goroutines • OR it is a function executing in parallel with other goroutines in the same address space • It's not the same as a thread, coroutine, process, etc. It's a goroutine. [New term]
Goroutines • Goroutines are cheap. • at least for now, goroutines are pthreads • BUT In 6g , they're multiplexed onto threads. • So, one OS Thread could handle more than one goroutines [Lightweight thread] • Their stacks are small (a few kB) and grow as needed.
Goroutines • Example in C (Threads) • In Go, Goroutines are cooperatively scheduled by the Go scheduler
Goroutines • A running program consists of one or more goroutines. • Their design hides many of the complexities of thread creation and management. (mutex, queues etc.) • When a goroutine executes a blocking system call, no other goroutineis blocked.
Communication (Channels) • Go has a type called a channel that provides communication and synchronization capabilities. • A channel provides a mechanism for two concurrently executing functions (goroutines) to synchronize execution and communicate by passing a value of a specified element type
Communication (Channels) • In its simplest form the type looks like this: chanelementType So to create a channel that can pass an integer : varchannel_object_name= make(chanint) • communication operator : <- channel <- value \\ send value var get = <-channel \\ receive value
Communication (Channels) • Unbuffered channel ch:= make(chanint) \\ one element only • Buffered channel: ch := make(chan type, value) • value : determine how many elements can be held