Channels

4 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
This is a free item
$0.00
د.إ0.00
Kz0.00
ARS$0.00
A$0.00
৳0.00
Лв0.00
Bs0.00
B$0.00
P0.00
CA$0.00
CHF 0.00
CLP$0.00
CN¥0.00
COP$0.00
₡0.00
Kč0.00
DKK kr0.00
RD$0.00
DA0.00
E£0.00
ብር0.00
€0.00
FJ$0.00
£0.00
Q0.00
GY$0.00
HK$0.00
L0.00
Ft0.00
₪0.00
₹0.00
ISK kr0.00
¥0.00
KSh0.00
₩0.00
DH0.00
L0.00
ден0.00
MOP$0.00
MX$0.00
RM0.00
N$0.00
₦0.00
C$0.00
NOK kr0.00
रु0.00
NZ$0.00
S/0.00
K0.00
₱0.00
₨0.00
zł0.00
₲0.00
L0.00
QR0.00
SAR0.00
SEK kr0.00
S$0.00
฿0.00
₺0.00
$U0.00
R0.00
ZK0.00
Already have an account? Log In

Transcript

HelloWorld at the heart of every concurrent problem is a producer and a consumer. So, in our paradigm, our producer is a goroutine, which runs independently and a consumer is also a goroutine which runs independently of the producer. So, the use case is that the producer produces some items which the consumer has to consume, right. So, these are these items are consumed in the first come first serve basis or FIFO basis. And so, it makes better sense to consume these items in a queue. So, this problem this problem is, if there is one producer if there is one producer and one consumer, what what happens if we have multiple consumers?

So, we have number one, we have consumer number two and so on. So, then it becomes a synchronous synchronicity problem because the producers and the humans need to be synchronous with a synchronized with each other. So, we have one thing that should be assigned to this but what happens when both of the consumers try to grab it So here's where channel come in. So channel, what channel is, is it's just, it's just a queue with mutexes. So when we're talking about goroutines and channels paradigm and Gaulin, then keep in mind that whoever talks about goroutines and channels internally is talking about basically a queue and a set of new Texas. So, whatever these items that the producer produces, they go inside the channel.

And then from inside the channel, we can control which consumer gets what. So, this is the theory behind channels and channels can be of multiple types. So they can be one channel, which is buffered. They can be buffered channels, and then they can be unbuffered channels. So unbuffered channels is a channel with whose queue sizes one. And queue size being one or use has been zero rather means that whenever a producer a producer produces something it can't produce anything else.

For this is consumed by the consumer. So let's say we have a queue, a queue is this. And a queue can only hold one item over here. Or it can only hold one item over here, the producer producer produces the item, and the consumer has to consume this item. So our little gopher over here he just consumed this. And then now producer is free to produce one more item.

So this is unbounded, this is a bounded buffer with zero size. And then we have unbounded buffers with a certain limit or limited buffers, which we can hold about two or three q two or three items in the queue. So, a producer produces two items, then the consumer on consumes the first item and then producer can produce one more item while this item is still in the queue. And then the second consumer consumes this item in our first come first serve basis. So this is behind this is a theory behind synchronous and asynchronous queues. This whole process is called inter process communication.

So we have one process, and we have two processes, and we have multiple consumers or we can have multiple producers also. So all of these are independent of each other. So they need to be synchronized with each other. And they need to communicate with each other in a process called inter process communication, which happens in golang using channels. So one more great thing about channels is that it's not like they're using a shared memory. So, channels can channels or data rather, can directly be passed from one go routine to the other goroutine because everything is stored in the form of pointers or references.

So every item that the producer produces is actually a kind of a reference, which can be consumed directly by the consumer and it does does not need to access it from a shared memory pool. So in this video, we talked about producers consumers channels go routines, and how goroutines and channels paradigm is just another name for queues and mutex. This

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.