Atomics - Part 1 - Atomics Basics (sync/atomic) (Concurrency in Go #5)

  Рет қаралды 7,199

Steve Hook

Steve Hook

Күн бұрын

In this video you'll learn everything about Atomics in Go. How to use atomic functions, atomic.Value and explore a bunch of examples & explanations alongside with issues & benchmarks. By the end of this video you'll confidently know everything about Atomics in Go.
I've got tons of surprises for you, so go get that zip archive and I'll see you in the video.
#steevehook #golang #concurrency #atomics
Enjoy 🚀💻🚀
00:00 - Intro
01:06 - Atomicity
03:39 - CPU Registers
04:57 - Go Routines & CPU Registers
05:34 - Example 1 - Race Condition
07:45 - Atomics Intro
08:10 - Example 2 - Basic Counter
11:31 - Example 3 - Race Fixed
12:41 - atomic.Value Intro
13:07 - Example 4 - Reader/Writer
17:44 - Example 4 - Calculator
22:16 - Example 5 - atomic.Value - Panic
23:39 - Example 6 - atomic.Value - Not Atomic
27:38 - Benchmarks
30:31 - Outro
✍ RESOURCES:
► github.com/golang-basics/conc...
💰 Support me on Patreon:
► / steevehook
💰 Paypal Donation:
► paypal.me/steevehook
💰 Buy me a Coffee:
►www.buymeacoffee.com/steevehook
💬 Discord COMMUNITY:
► / discord
🐧 FEEDBACK:
► gophertuts.typeform.com/to/j2...
💻 GITHUB:
► github.com/gophertuts
► github.com/steevehook
💻 Repl.it:
► repl.it/@steevehook
💻 Exercism:
► exercism.io/profiles/steevehook
📖 Medium:
► / steevehook
💬 TWITTER:
► / @steevehook
💬 FACEBOOK:
► / steevehookmd
💬 REDIT:
► / steevehook
💬 INSTAGRAM:
► / steevehook
✉️ EMAIL
► steevehook@gmail.com
⚡ WEBSITE:
► www.steevehook.com
🎧 Music:
► www.epidemicsound.com/referra...
► artlist.io/Steve-411836
🎥 Gear used:
► Sony 𝛼7III
► Rode NTG-3
► Golden Age Project Pre73-JR preamp
______________________________________________________________________________
Software engineering is one of the most accessible fields nowadays, but it's easy to get lost in the huge amount of technologies and ways to approach engineering problems. Lots of languages, frameworks, solutions and ways to be a productive engineer.
I share my own experience on what is like to be a software engineer and the skills required to be a productive one. On this channel I share my thoughts, opinions and video tutorials about topics related to a certain technology, to make it easier for you to ultimately make a choice or just get better at software engineering by learning from my mistakes/experience.
Make sure to also check out 5m Friday every Friday, where I share my thoughts and opinions about different engineering topics. I'll c you around 😎
Stay tuned!
🚀💻🚀

Пікірлер: 26
@yashgupta-dw7sn
@yashgupta-dw7sn 3 ай бұрын
amazing video, no waste of time, to the point. keep it up man!!
@YashGupta-kx7pl
@YashGupta-kx7pl 2 жыл бұрын
Excellent video!!! completely conceptual and to the point.
@SteveHook
@SteveHook 2 жыл бұрын
Thanks a lot Yash 😌
@GerbenWijnja
@GerbenWijnja 3 жыл бұрын
Thanks for the no-nonsense explanation and examples. I'm getting Derek Banas flashbacks. Good stuff.
@SteveHook
@SteveHook 3 жыл бұрын
Thanks mate 😻😻 although I still gotta grow to be at his level 🙀
@rouabahoussama
@rouabahoussama 3 жыл бұрын
Thank you, great video 👌🏾
@SteveHook
@SteveHook 3 жыл бұрын
You’re welcome ☺️😉
@3x10.8_ms
@3x10.8_ms 9 ай бұрын
gem
@YashGupta-kx7pl
@YashGupta-kx7pl 2 жыл бұрын
Hey Steve, one question: how does the output get consistent when the code is triggered with a race flag? Any help would be appreciated. I am very new to this, but I only know that a race flag is used to detect if a race condition exists or not.
@SteveHook
@SteveHook 2 жыл бұрын
Hey Yash. When using the race flag the output is not always consistent. When using -race it instructs Go to do a bunch of extra checks which makes it work slower, thus resulting in an output as if we’re not running things concurrently. Increasing the number of go routines will give you a bigger probability of getting different results. I used a relatively small number to keep things simple without extra bloat. Hope this answers your question 👊
@YashGupta-kx7pl
@YashGupta-kx7pl 2 жыл бұрын
​@@SteveHook Okay, If the variable "res" isn't of the type atomic.Value then executing the code with the -race flag will return the race condition because the goroutines will race towards each other. But now when using the variable "res" is of type atomic.Value then the value is determined by which go routine is executed first, and the -race flag doesn't return the race condition error and returns a specific value? Am I going in the right direction?
@SteveHook
@SteveHook 2 жыл бұрын
Yes. All that the -race flag does is it adds breakpoints and executes the program for you passing through those breakpoints, sometimes a piece of code may have race conditions, it just does not run through them, so it depends how you’re using the final concurrent code. But whether you’re using -race or not as long as the code is run concurrently, meaning multiple dudes attempt to change the value of a variable, it should in theory give you different results. The race flag just runs slower, a lot of the times resulting in the same result. I use -race just for checks, not for checking the result difference, but like I said, depends on how you consume the running piece of concurrent code, sometimes it may not detect any race conditions, simply because it does not run through the breakpoints 😏
@YashGupta-kx7pl
@YashGupta-kx7pl 2 жыл бұрын
Thanks a lot, now it makes sense. I ran the code multiple times and got different answers with -race flag as well. It clears my doubt.
@SteveHook
@SteveHook 2 жыл бұрын
Glad I could help ya sir 🤓
@tkg__
@tkg__ 3 жыл бұрын
I think there is a mistake in the fixed example with the counter. Store is not the equivalent of Add. And even if we use Add the goroutines could run in different order, and we'd get different results regardless of the atomicity of the operations. Or am I wrong? I made a simple example to test it.
@SteveHook
@SteveHook 3 жыл бұрын
Yes you’re right. Normally I had to use Load then Store or use Add. I kept things simple by using just one function, to save a little bit of time. Yes order is never guaranteed, normal concurrent code ☺️ The whole idea of the fixed example is to keep things atomic & avoid the data race, covering every single aspect would’ve ended in a longer video 😅 Atomic does not necessarily mean preserving the order
@tkg__
@tkg__ 3 жыл бұрын
@@SteveHook Gotcha. I got confused for a second, but now it makes sense. 🥳
@johnnychang3456
@johnnychang3456 2 жыл бұрын
In the Reader/Writer example, how does the go routine with the infinite loop stops? Without sending any signal, it seems like the go routine will run infinitely? How does this code actually work?
@SteveHook
@SteveHook 2 жыл бұрын
Could you post a timestamp of the video. I happen to forget the context of my super long videos 🤣
@johnnychang3456
@johnnychang3456 2 жыл бұрын
17:03 example 4
@SteveHook
@SteveHook 2 жыл бұрын
So the go routine with the infinite loop in the example 4 will stop with the main go routine. When the program terminates. The whole purpose of that go routine is to update the value at a very fast rate, and prove it will 💯 be concurrent safe to run using atomics. The goal was not to gracefully shut down each go routine, and keep the code simple. Of course I could’ve added some kind of select in the for and cancel them, but that would’ve made it more complicated, I try to deviate as little from the topic so that examples are short and to the point without introducing new constructs or features that will be covered in future videos. Hope this answers your question ☺️☺️
@johnnychang3456
@johnnychang3456 2 жыл бұрын
@@SteveHook Yes. Thank you for the prompt response!
@SteveHook
@SteveHook 2 жыл бұрын
Anytime mate 🤟
@VitalyZdanevich
@VitalyZdanevich 2 жыл бұрын
newCalculator(): maybe just new()?
@SteveHook
@SteveHook 2 жыл бұрын
When it comes to naming things, I prefer anything that’s simple and descriptive while it does not conflict or appear as built in keywords, for instance new, make, append and so on. Hope this explains my choice 😊
@VitalyZdanevich
@VitalyZdanevich 2 жыл бұрын
@@SteveHook Oh, sorry, not new() but New(), or newT() for private
Atomics - Part 2 - Go ASM (Go Assembly) (Concurrency in Go #6)
27:28
Introduction to Concurrency (Concurrency in Go #1)
27:54
Steve Hook
Рет қаралды 30 М.
Llegó al techo 😱
00:37
Juan De Dios Pantoja
Рет қаралды 58 МЛН
How Many Balloons Does It Take To Fly?
00:18
MrBeast
Рет қаралды 199 МЛН
Slow motion boy #shorts by Tsuriki Show
00:14
Tsuriki Show
Рет қаралды 9 МЛН
HTMX - What they don't want you to know!
13:28
CoderOne
Рет қаралды 82 М.
Why I'm learning Go
21:35
Web Dev Cody
Рет қаралды 60 М.
WaitGroups - Part 1 (Concurrency in Go #2)
17:56
Steve Hook
Рет қаралды 11 М.
Golang Channels Or Wait Groups? Let Me Explain.
18:32
Anthony GG
Рет қаралды 20 М.
WaitGroups - Part 2 (Concurrency in Go #3)
14:34
Steve Hook
Рет қаралды 7 М.
SOLID in Go in 8 minutes (5m Friday #5)
8:52
Steve Hook
Рет қаралды 26 М.
Concurrency in Go
18:40
Jake Wright
Рет қаралды 611 М.
DO NOT USE sync.Map in Go!
9:14
rwxrob
Рет қаралды 7 М.
Avoid golang interviews unless you know these 3 things
6:38
Kantan Coding
Рет қаралды 2,9 М.
Опасность фирменной зарядки Apple
0:57
SuperCrastan
Рет қаралды 11 МЛН
low battery 🪫
0:10
dednahype
Рет қаралды 1,5 МЛН