GO Language Learning Road 21

2022/02/12

/*

//Basic introduction to processes and threads

1. A process is an execution of a program in an operating system.

Is the basic unit for system resource allocation and scheduling

2. Threads are an execution instance of a process and the smallest unit of program execution.

* It is a smaller base unit that can run independently than a process

3. A process can create and destroy multiple threads, and multiple threads in the same process can execute concurrently

4. A program has at least one process and a process has at least one thread

//goroutine basic introduction

1. Multithreaded programs run on a single core, meaning concurrent

* Because it's on a single cpu, such as 10 threads, each performing 10 milliseconds (in turn)

From a human perspective, it looks like all 10 threads are running, but at a microscopic point in time,

There is actually only one thread executing, which is concurrency

2. Multithreaded programs run on multiple cores, which means parallel

Because it is on multiple cpUs, such as 10 cpu s, such as 10 threads, each executing for 10 milliseconds,

These 10 threads are running from a human perspective, but from a micro perspective

On the top, at an event point, there are also 10 threads executing at the same time, which is parallelism

//Go Co-operation and Go Main Thread

1.go main thread (programmers directly become threads, or processes), on a go thread,

Multiple protocols can be started, which can be understood as lightweight threads

2. Features of the Go Protocol

Has separate stack space, shares program stack space, schedules are user controlled, and collaborations are lightweight threads

*/

package main

import (
	"fmt"
	"strconv"
	"time"
)

/*
	In the main thread, open a goroutine that outputs "hello,world" every second
	In the main thread, output "hello,golang" every second, after 10 output, exit the program
	Require that the main thread and goroutine execute simultaneously
*/

func test() {
	for i := 0; i < 10; i++ {
		fmt.Println("hello,world" + strconv.Itoa(i))
		time.Sleep(time.Second)
	}
}

func main() {
	go test() //Opened a protocol
	for i := 0; i < 10; i++ {
		fmt.Println("~~~hello,golang" + strconv.Itoa(i))
		time.Sleep(time.Second)
	}
	//If the main thread exits, the collaboration exits even if it has not finished executing
}
package main

import (
	"fmt"
	"runtime"
)

/*
	1.The main thread is a physical thread that acts directly on the cpu, is heavy and consumes CPU resources
	2.A collaboration is opened from the main thread, is a lightweight thread, is logical, and consumes relatively little resources
	3.golang The protocol mechanism is an important feature that makes it easy to open tens of thousands of protocols, other programming languages
		The concurrency mechanism is generally thread-based, with too many threads open and high resource consumption, which is highlighted here
		golang Concurrency advantages
	4. MPG What's this
		M: The main thread of the operating system (is a physical thread)
		P: Context context required for collaboration execution
		G: Protocol
	5.golang To take full advantage of the advantages of multiple cpUs, you can set the number of running cpUs in the golang program
		At go1. After 8, let the program run on multiple cores by default, you can set up the runtime package without setting it up
*/

func main() {
	cpuNum := runtime.NumCPU()
	fmt.Println("cpuNum = ", cpuNum) //cpuNum =  12

	//You can set up to use more than one cpu
	//runtime.GOMAXPROCS(cpuNum)
}
package main

import (
	"fmt"
	"sync"
	"time"
)

/*
	The factorial of the numbers 1-20 is required and the factorial results of the numbers are put into the map
	Last shown, requiring goroutine to complete
*/

var (
	myMap = make(map[int]int, 10)
	lock  sync.Mutex
)

func test(n int) {
	res := 1
	for i := 1; i <= n; i++ {
		res *= i
	}
	lock.Lock()
	myMap[n] = res
	lock.Unlock()
}
func main() {
	//Loop opens 20 coprocess calculations
	for i := 0; i < 20; i++ {
		go test(i)
	}

	//Hibernate for five seconds to try
	time.Sleep(time.Second * 5)
	// lock.Lock() // lock while reading [possibly] because the main thread does not know if processing is over
	for i, v := range myMap {
		fmt.Printf("map[%d] = %d\n", i, v) //fatal error: concurrent map writes
	}
	// lock.Unlock()

	/*
		Two problems arise above
		1.When data is written, there is competition for resources!
		2.How long is a good time to sleep?

		Via go build-race main. Go
		Visually there are two competing resources
	*/

	/*
		1.Solution 1 Using global variable lock synchronization improver
		---- Use mutexes in sync packages
		//Lock declares lock sync. Mutex
		Lock up. Lock()
		Unlock lock.Unlock()

		2.Solution 2===========channel pipeline
		//Look again next time
	*/
}

Keywords: Go linq

Added by php_coder_dvo on Sat, 12 Feb 2022 19:38:12 +0200