Go language about threading and channel channal

Source: Internet
Author: User

In the go language, the use of multithreading has been encapsulated to make it easy and usable.

Here to talk about their own experience, the wrong place is still ask you to correct me.

The concurrency mechanism for the Go language, which is simple, add go before the function you want to execute

Like what:

Package Main

Import ( "fmt") Func main () { go fmt. Println ("1") FMT. Println ("2") }

Ok so that can be used, but this program will run the problem, you will find that 1 printing, why?

You asked me why?

This is because in the execution of this, you can imagine that the program first executes the main thread, then print out 2, and then the program exits, this time the program has not come and print the next 1, you quit so you do not see!!

So how do you see it?

Add a delay, wait until the thread finishes the job, and you can see it, like this.

Package Mainimport (         "fmt") Func main () {    go fmt. Println ("1")    FMT. Println ("2")    
Time. Sleep (time. second*1)}

So you will find that, in fact, the program first print 2, in print 1, because it is the main thread first, so the situation will appear (I guess, you believe it or not, OK this is true, you ask who is this to you)

Anyway, I'm going to ask you. This multithreading is easy to learn. You think it is CreateThread () cool, or this cool!

Let's talk about channel Channal.

You can understand this as a channel of communication between multiple threads.

You asked me how to use this thing?

Ch:=make (chan int)

Chan is the key word for the channel.

Make that is the meaning of creation

int that's a channel of type int

Simple is not easy, happy not happy?

It's too early for you to be happy, hahaha haha

Above is just the basic usage, there is something to be said in detail, otherwise you still do not know how to use Chan to do things!

Above the

CH: = make (chan int)

is to create a non-buffer int type Chan, specifically what is called no buffer, the amount, do not know how to explain, later on.

Ch: =make (<-chan int)

This is no buffer read-only CH

Ch:=make (chan<-int)

No buffer write-only Chan

About the "<-" symbol, which is often encountered in multiple threads of go, you need to know what this is for

According to my understanding:

ch<-

This means that data is written to Ch

<-ch This means reading the data out of Ch.

Or a small example of something related:

Package Mainimport (    "fmt"//    "Time") func main () {    ch:int)    ch<-1    go func () {        <-Ch        FMT. Println ("1")    } ()    FMT. Println (": 2")}

After the run will tell you, deadlock, why does it appear? He is no buffer, that is, the assignment must go in to take a value. Or it's a dead lock.

Package Mainimport ("FMT"//"Time" "Time") Func main () {Ch:=make (chan int,1) ch<-1go func () {v:=<-chfmt. Println (v)} () Fmt. Println (": 2") time. Sleep (time. SECOND*1)}

Try it this way!

Package Main
import (

"FMT"

/ /"Time"
" Time "
)
func main () {

Ch:=make (chan int"



go func () {
v:=< -ch
FMT. Println (v)
} ()
Ch<-1
FMT. Println ( ": 2")


}

Or so, the back of this is no buffer, so that the CH in the assignment when the time is blocked, know gofunc to take away, so print out the result is

Production consumers:

Import (

"FMT"
"Time"
)
FuncProduce (Pchan<- int){     
for i: =0; i<; i+ +{      
P<- i 
FMT. Println("Send:", i)     
}
}
FuncConsumer (c<-chan int){     
for i: =0; i<; i+ +{      
V: =<-c 
FMT. Println("Receive:", v)     
}
}
FuncMain (){ 
CH: =make(chan int)     
go produce(ch)   
Go consumer(ch)   
Time. Sleep(1* time. Second)      
}

This example, I think can reflect the more obvious, no buffer means that this is synchronous, that is, can only be ch write, take out, write, remove, write, remove such walk, no buffer must be guaranteed one write, another take out, in order to perform the next

And look at this:

Import (

"FMT"
"Time"
)
FuncProduce (Pchan<- int){     
for i: =0; i<; i+ +{      
P<- i 
FMT. Println("Send:", i)     
}
}
FuncConsumer (c<-chan int){     
for i: =0; i<; i+ +{      
V: =<-c 
FMT. Println("Receive:", v)     
}
}
FuncMain (){ 
CH: =make(chan int,ten)       
go produce(ch)   
Go consumer(ch)   
Time. Sleep(1* time. Second)      
}


Take a look at this, this is a buffer, what do you mean?

Is that I have a container, I can always go inside production, know that this is filled, that is, I can always go to this buffer to plug things, until the 10 are filled, I can always read the data, but also can be taken out.

That's probably what this means.

Reference Documentation:

http://studygolang.com/articles/3311

Go language about threading and channel channal

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.