-
Notifications
You must be signed in to change notification settings - Fork 13
/
goroutines.go
67 lines (57 loc) · 3.41 KB
/
goroutines.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
==================what are go routines ?
A goroutine is a lightweight thread managed by the Go runtime.
using go routines we can make our sequential program into concurrent program
=====================================goroutines vs threads ?
GOROUTINE THREAD
1.Goroutines are managed by the go runtime. 1.Operating system threads are managed by kernal.
2.Goroutine are not hardware dependent. 2.Threads are hardware dependent.
3.Goroutines have easy communication medium known as channel. 3.Thread doesnot have easy communication medium.
4.Due to the presence of channel one goroutine can 4.Due to lack of easy communication medium inter-threads communicate .
communicate with other goroutine with low latency. takes place with high latency
5.Goroutine doesnot have ID because go doesnot have 5.Threads have their own unique ID because they have Thread Local Storage.
Thread Local Storage.
6.Goroutines are cheaper than threads. 6.The cost of threads are higher than goroutine.
7.They are cooperatively scheduled. 7.They are preemptively scheduled.
8.They have fasted startup time than threads. 8.They have slow startup time than goroutines.
9.Goroutine has growable segmented stacks. 9.Threads doesnot have growable segmented stacks.
===================why goroutines are lightweight process?
A goroutine is created with initial only 2KB of stack size.
Each function in go already has a check if more stack is needed or not and the stack can be
copied to another region in memory with twice the original size. This makes goroutine very
light on resources.
================why goroutines are hardware independent?
Goroutines exists only in the virtual space of go runtime and not in the OS
=======================Blocking in go routines?
a Go Runtime scheduler is needed which manages their lifecycle.
Go Runtime maintains three C structs for this purpose:
1.The G Struct : This represents a single go routine with it’s properties such as stack pointer, base of stack, it’s ID, it’s cache and it’s status
2.The M Struct : This represents an OS thread. It also contains a pointer to the global queue of runnable goroutines, the current running goroutine and the reference to the scheduler
3.The Sched Struct : It is a global struct and contains the queues free and waiting goroutines as well as threads.
=====================Goroutines c structs
If a goroutine blocks on system call, it blocks it’s running thread.
But another thread is taken from the waiting queue of Scheduler (the Sched struct) and
used for other runnable goroutines.
However, if you communicate using channels in go which exists only in virtual space,
the OS doesn’t block the thread. Such goroutines simply go in the waiting state and other
runnable goroutine (from the M struct) is scheduled in it’s place.
*/
package main
import (
"fmt"
"time"
)
func number(num int) {
for i := 0; i < num; i++ {
// wait function which will wait for 1 sec before printing
time.Sleep(time.Second)
fmt.Println(i)
}
}
func main() {
//can make a function goroutine and concurrent by adding go keyword as prefix to a function call
go number(5)
go number(5)
//it is added so that our main function do not terminate before execution of our go routines
fmt.Scanln()
}