One. Concurrency & Parallelism
An application---> a process---> a standalone executor running in its own memory address space---> Multiple threads working together with a memory address space
A concurrent program---> Multiple threads to perform tasks---> running at a point in time on multicore or multiprocessor---> concurrent & Parallel
---> A point in time running on a single processor--\---> Concurrency & not parallel
Parallelism is the ability to increase speed by using multiple processors. So concurrent programs can be either parallel or not.
Admittedly, the use of multi-threaded applications is difficult to achieve accurate, the main problem is in-memory data sharing, they will be multi-threaded in an unpredictable way to operate, resulting in some can not reproduce or random results (called 竞态 )
concurrency mode:
1. Deterministic (clearly defined sort)
2. Non-deterministic (locking/Mutex thus undefined sort)---> Race
Instead of using global variables or shared memory, they can put your code at risk when it comes to concurrency.
The way to solve it:
1. Synchronize different threads and lock the data so that only one thread can change the data
2. One is known Communicating Sequential Processes(顺序通信处理) as (CSP, C. Hoare invented)
3. There is also a name message passing-model(消息传递) (already used in other languages, such as Erlang)
Two. Go's co-process
In Go, the part of the application concurrency process is calledgoroutines(协程)
The way---> work in the same address space---> shared memory must be synchronous ; This can be sync implemented using packages (not recommended)
---> Use channels to synchronize co-processes
Characteristics:
1. Use a small amount of memory and resources: Use 4K of stack memory to create them in the heap
2. The stack is segmented to dynamically increase (or reduce) the use of memory, the management of the stack is automatic, but not managed by the garbage collector, but automatically released after the process exits
3. The process can run between multiple operating system threads , or it can run within a thread
4. With a small number of operating system threads, you can have any number of service-servicing processes , and the Go runtime can intelligently realize which processes are blocked, shelve them, and handle other processes
5. There are two concurrency modes: deterministic (clearly defined sort) and non-deterministic (locking/Mutex thus undefined ordering). The Go process and the channel naturally support deterministic concurrency (for example, the channel has a sender and a receiver)
Implementation form:
Keyword go call---> A function or method---> Start a simultaneous function in the current calculation---> in the same address space and allocated a separate stack ( stack split )
The stack will scale as needed, without stack overflow, and developers don't have to worry about the size of the stack. When the process ends, it exits silently: the function used to start the process does not get any return value
Three. Go coprocessor Parallel:
Go defaults to no parallel instruction, only a single core or processor is dedicated to go, no matter how many threads it initiates, so these are run concurrently, but they are not running in parallel: only one of the threads is running at the same time
Under the GC compiler (6g or 8g) you must set Gomaxprocs to a value greater than the default value of 1 to allow the runtime to support more than 1 operating system threads, all of which will share the same thread unless Gomaxprocs is set to a number greater than 1. When Gomaxprocs is greater than 1 o'clock, there is a thread pool that manages many threads. gccgoThe compiler Gomaxprocs is valid with the number of running threads equal. Suppose N is the number of processors or cores on a machine. If you set the environment variable gomaxprocs>=n, or execute runtime.GOMAXPROCS(n) , then the process will be split (dispersed) to n processors. More processors do not imply a linear improvement in performance. There is such a rule of thumb that setting Gomaxprocs to n-1 for optimal performance for n-core situations is also subject to the same rules: number of threads > 1 + gomaxprocs > 1.
So if at some time only one of the processes is executing, do not set gomaxprocs!
There are some experiments observed: on a 1-CPU laptop, an increase of gomaxprocs to 9 will result in a performance boost. On a 32-core machine, setting the gomaxprocs=8 will achieve the best performance, and in a test environment, higher values cannot improve performance. Setting a large gomaxprocs only brings a slight performance drop; set gomaxprocs=100, using top commands and H options to view only 7 active threads.
It is advantageous to increase the numerical value of gomaxprocs to calculate the concurrency of the program;
Summary: Gomaxprocs equals the number of threads (concurrent), and on a machine with more than 1 cores, the threads that are equal to the number of cores are run in parallel as much as possible.
Go co-process