- الحصول على الرابط
- X
- بريد إلكتروني
- التطبيقات الأخرى
- الحصول على الرابط
- X
- بريد إلكتروني
- التطبيقات الأخرى
When you're using Goroutines, the Go routine doesn't know anything about the timing of other Goroutines. So if a Go routine is executing some code, it knows what line of code it's at, it doesn't have any idea what line of code the other Goroutines are at and it doesn't care.
So normally, these Goroutines don't have any understanding of the timing going on in the other Goroutines. But synchronization basically breaks that.
Synchronization says,, we're going to make some kind of a global event that every thread sees or every Go routine sees at the same time. These are important to restrict order, to restrict some of the different interleavings that are possible.
So concurrency, the beauty of concurrency and parallelism is that the interleaving is arbitrary. In fact, if it's parallel, things can run exactly at the same time but even if it's not parallel, it's just concurrent, the ordering is arbitrary, we don't care about the ordering and that gives us a lot of advantages, it allows us to speed up the execution of the code.
With concurrency, if the order doesn't matter, the scheduler can just move in another thread in the meantime while the first one's waiting. So we get a lot of optimization, a lot of speed improvement when we don't restrict the scheduling.
By using synchronization, we are restricting the scheduling. So understand that every time we use synchronization, we are reducing the efficiency, we're reducing the amount of possible concurrency. So we're reducing the options for the scheduler so the scheduler won't be able to use the hardware as well as effectively.
So there might be times when nothing can execute because we're waiting on the synchronization events. So in general, synchronization is bad because it reduces your performance and your efficiency but it is necessary for cases like this where you have to restrict the ordering. Certain things have to happen in certain orders.
Synchronization is necessary for particular situations and it is complicated to use, although Golang makes it pretty easy. anyway, synchronization is bad but necessary.
Golang provides a synchronization package to help us to achieve synchronization between goroutines.
Sync Package has a lot of different synchronization functions built into it, for example WaitGroup
WaitGroup forces a goroutine to wait for other goroutines. So, WaitGroup is a basic group of you can think of it as a group of goroutines that your goroutine is waiting on. Your goroutine will not continue until all goroutines in the WaitGroup are finished.
So, this is what we need for the example that we had before, remember that print a message and then I had the new goroutine print the message and the new goroutines message never got printed because the main goroutine executed and completed before the new goroutine had a chance to execute.
So, what we want to do is make this main goroutine wait until the new goroutine finishes. then the main routine can continue. In that way, we make sure that this new goroutine has the chance to actually execute before the main goroutine completes.
- الحصول على الرابط
- X
- بريد إلكتروني
- التطبيقات الأخرى
تعليقات
إرسال تعليق