Download presentation
Presentation is loading. Please wait.
Published byDimitri Shield Modified over 9 years ago
1
Go Language * Go - Routines * Channels
2
New Concepts Do not communicate by sharing memory; instead, share memory by communicating. creating shared memory VS sharing by communication channels. Lightweight Threads (Goroutines)
3
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]
4
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.
5
Goroutines Example in C (Threads) In Go, Goroutines are cooperatively scheduled by the Go scheduler
6
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 goroutine is blocked.
7
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
8
Communication (Channels) In its simplest form the type looks like this: chan elementType So to create a channel that can pass an integer : var channel_object_name = make(chan int) communication operator : <- channel <- value \\ send value var get = <-channel \\ receive value
9
Communication (Channels) Unbuffered channel ch := make(chan int) \\ one element only Buffered channel: ch := make(chan type, value) value : determine how many elements can be held
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.