Race Conditions in C# .NET Core

  Рет қаралды 10,511

Raw Coding

Raw Coding

Күн бұрын

Пікірлер: 64
@RawCoding
@RawCoding 2 жыл бұрын
Source code for patreon supporters includes an example of how to simulate race conditions using Threads. www.patreon.com/raw_coding
@Sad-Lemon
@Sad-Lemon 2 жыл бұрын
Anton, your channel is so underrated... I hope you hit 1M subs pretty soon! Thank you mate!
@RawCoding
@RawCoding 2 жыл бұрын
Hopefully one day :D
@ivandrofly
@ivandrofly 6 ай бұрын
11:39 - I think the explanation about thread starvation is a bit confusing … But here is my understanding: 10 Tasks 5 Threads 1 thread made it through the locks 4 threads waits (each one with a task assigned) Threads #1 (in the lock) go to read file see async/await return to thread pool then it get assigned a new task task#6 comes and see the lock blocks too! Important: Assuming the thread-pool size is 5; if not then a thread from the thread-pool will just be assigned to the file-writing that was previously started by thread#1 and leave the lock. Ps: For those curious to see this simulation, make sure U set the ThreadPool.SetMinThreads and also ThreadPool.SetMaxThreads
@sauravbhatta5303
@sauravbhatta5303 2 жыл бұрын
Great content Anton. Learning from you is great.
@RawCoding
@RawCoding 2 жыл бұрын
Glad you like it!
@Vlad-zr1fr
@Vlad-zr1fr Жыл бұрын
Hello, Anton Finally, I found a person who shows real things that are really useful for production, and not another set of CRUDs tutorials with a repository pattern (around EF) and 3-layer architecture. By the way, the guys from dev mentors also have very good content and trying to provide REAL things. It's not fair that your channel is so underrated, because people just need the simplest things, e.g python bases, how to parse a json files or create a bot that sends hello world (I'm a cool and useful bot (no)). I worked for a long time as a senior and lead software engineer in OLTP systems, microservices, DDD and etc. It was all around dotnet, C#, azure environment. 2 years ago, I had a situation on a project where I had to migrate data from PostgreSQL to ClickHouse due to the performance of query execution and thus moved to OLAP systems, big data, python, spark, airflow, azure data factory etc., where a lot is built around python, SCALA, R, java and so on And it's really a shame that dotnet is quite capable of being in the same category, and in terms of performance, it's definitely one of the leaders. I don’t know if you have worked with Spark or not, but Microsoft has github.com/dotnet/spark, so do you believe in the future of dotnet in the context of big data, machine learning, etc? Thank you
@RawCoding
@RawCoding Жыл бұрын
Cheers, I think .NET is doing pretty good and will continue to do so. It has an amazing community and from what I've seen amongst other languages (java/python/js/clojure) c# has one of the best set of tools and frameworks out there.
@t400ml
@t400ml 2 жыл бұрын
5:00 I'm still a bit uncertain as to why the Task.WhenAll isn't sufficient to have the number get to 1000 before the Console.WriteLine?
@RawCoding
@RawCoding 2 жыл бұрын
It is and isn't. If we do Task.Run it kicks off the process in parallel so that means it may execute the function before we schedule the next task, so on as so forth, if all tasks complete before we schedule the next task they will all complete sequentially
@numannebuni
@numannebuni 2 жыл бұрын
@@RawCoding I'm super confused too. I was expecting "await Task.WhenAll(tasks)" to wait for all tasks to complete and therefore the final result to be 1000. So what happens here is that when the for loop finishes, the "tasks" variable doesn't hold all tasks?
@RawCoding
@RawCoding 2 жыл бұрын
Task.Run places the function to run on a thread, you are scheduling a task, just because you wait for all the tasks to complete doesn’t mean they will all complete in order or won’t run over each other. If we schedule 1000 tasks faster than any of them start running, some may start at the same time and cause a race condition or they may all run sequentially, we don’t know.
@brunogiovagnoli3022
@brunogiovagnoli3022 Жыл бұрын
I have been watching Anton since the beginning of his channel and it is amazing to see how much he has improved the quality of his content. Each time he explains better and in less time. Community: How could we help him get more popularity and views? Can you imagine Anton dedicating himself to KZbin full-time? 😱
@RawCoding
@RawCoding Жыл бұрын
thank you bro
@slamspam
@slamspam Жыл бұрын
outstanding video. great information & step-by-step demo. thanks
@chrismoutray9206
@chrismoutray9206 2 жыл бұрын
That last bit about distributed lock was useful 😎
@olegholostenco8736
@olegholostenco8736 2 жыл бұрын
That is awesome. Thanks Please, make similar video about deadlocks with real examples in real projects
@ivandrofly
@ivandrofly 9 ай бұрын
The last part about how you can use Redis to handle lock on a distributed system / distributed lock is very useful
@MrJonnis13
@MrJonnis13 2 жыл бұрын
Impressive ! Thank you Anton. At 12:15, could we bypass this effect by using *ConfigureAwait(false)* ?
@RawCoding
@RawCoding 2 жыл бұрын
No, threads processing the tasks would be blocked.
@MrJonnis13
@MrJonnis13 2 жыл бұрын
@@RawCoding so the 5 threads that processing the Tasks will be waiting the operation the finish, and even if the operation is done, there will be no available thread (since all are occupied) to finish up the initial Task. Did I get it right?
@ivandrofly
@ivandrofly 6 ай бұрын
7:31 - 2x Task
@josepvg
@josepvg Жыл бұрын
I love your videos, thanks for making them
@salehdanbous6783
@salehdanbous6783 Жыл бұрын
Regarding distributed locking, from what I saw people usually tend to use it to lock updating files and not concurrent database updates! When updating database would it be viable to use Distributed Locking? I heard some developers praise the use of FIFO queues like SQS, RabbitMQ and the like!
@RawCoding
@RawCoding Жыл бұрын
queues are a way to structure your application to avoid locking
@salehdanbous6783
@salehdanbous6783 Жыл бұрын
@@RawCoding Do you think using Redis Distributed Locking would be ideal for solving database update collisions? From your perspective what would be the optimal way in a distributed architecture?
@RawCoding
@RawCoding Жыл бұрын
@@salehdanbous6783 I think if you design well, you might not need any locking, but generally it will be queues (actor model, which is queues on steroids)
@akbaraliotakhanov1221
@akbaraliotakhanov1221 6 ай бұрын
Nice job! I have an issue with production app. I need your advice. Now, I have a transient class which receives an update request. The request may be single or multi which means parallel tasks. Inside this class, I used a method to write some .json file and shut down the running up to continue update on the another local app. Here is the problem, in case multiple requests how can I avoid parallel threds using a specific method? I tried with semaphoreSlim and locks but it doesn’t work perfectly
@FXK23
@FXK23 2 жыл бұрын
What is the difference between using the semaphore vs. redis-conditional, assuming only one thread active on the protected resource?
@RawCoding
@RawCoding 2 жыл бұрын
Semaphore is process scoped, redis is distributed
@imtheshadow6396
@imtheshadow6396 2 жыл бұрын
Hey Anton, thank you for the awesome video, keep it up! I have a related question that confusing me, when we declare our services as scoped, does that means the .Net will separate contexts between scopes and eliminate racing conditions between them, so one scope will not enter another scope context? And if you could create a video explaining scopes/transients in the context of threadings and race conditions that would be awesome, thank you!
@RawCoding
@RawCoding 2 жыл бұрын
Cheers, scoped/transient doesn’t do anything for race conditions - it’s service lifetime it’s about which object you get
@AndersBaumann
@AndersBaumann Ай бұрын
Nice video. If you are already using SQL server then you can use the Distributedlock library instead of Redis.
@nadab.657
@nadab.657 2 жыл бұрын
Out of context, what's the keyboard using ? That sounded nice. : ) as always top video. Thanks for getting and keeping my job.
@ghostofalexandria8914
@ghostofalexandria8914 2 жыл бұрын
It is a keyboard with red switches. ;)
@RawCoding
@RawCoding 2 жыл бұрын
It’s keychron with brown switches
@tonyschoborg
@tonyschoborg 2 жыл бұрын
I like the browns as well, blue switches are too obnoxious.
@CarrigansGuitarClub
@CarrigansGuitarClub 2 жыл бұрын
Good high-end topic, with suggestions for resolutions
@parapadirapa
@parapadirapa 4 күн бұрын
11min45sec. I didn't get the threads congestion example... First it's 10 tasks. Then it's 5 threads... And only one is awaiting the task... But then you speak of 5 tasks. The tasks number magically gets cut in half. Could anyone please rephrase the example, please? Also didn't get why the threads are actually starving.. I thought the whole point of a lock was to avoid concurrent access to a resource (and in so doing, relieving threads of congestion, not increasing the congestion). Also starving and congestion seem like very confusing terms as they seem to refer to opposing concepts.
@caglarcansarikaya1550
@caglarcansarikaya1550 2 жыл бұрын
thanks for it. I learnt semaphore and mutex :) why you didn't try Concurrent something is there any solution with it? I would like to try, I have just finished the video :)
@caglarcansarikaya1550
@caglarcansarikaya1550 2 жыл бұрын
and Parallel.For(0, 1000, (i, state) => { number++; }); this one is gives the 1000
@caglarcansarikaya1550
@caglarcansarikaya1550 2 жыл бұрын
that really interesting I have tried with concurrentbag and concurrentqueue and both of is messed up also I have tried with arrays, there is no difference
@caglarcansarikaya1550
@caglarcansarikaya1550 2 жыл бұрын
I also understand the mutex, Mutex works as await when it finished I saw many various values smaller than 1000 on the doc. I didn't understand one thing, appreciate this video and I also watched your async await video some time ago and still I remember how it works. Normal for loop finished in 1.2 sec by writing a file, async threads finished in 4.5 sec. Why should we use async threads? it works slower than the sync
@RawCoding
@RawCoding 2 жыл бұрын
not sure how you measured, but sync is faster than async. And you use async to avoid blocking, if you don't have async you have to use locks and threads.
@caglarcansarikaya1550
@caglarcansarikaya1550 2 жыл бұрын
@@RawCoding yes I found same results, sync is faster. :)
@Kevmoens
@Kevmoens Жыл бұрын
I’m assuming interlock would work too.
@obsidian741
@obsidian741 2 жыл бұрын
Hi Anton, what IDE are you using in this video?
@tonyschoborg
@tonyschoborg 2 жыл бұрын
It is Rider by JetBrains. Not free, but worth every penny.
@obsidian741
@obsidian741 2 жыл бұрын
@@tonyschoborg Hey Tony, could you provide some of the nice features VS doesn't have while Rider has? I know ReSharper and want to know more about it.
@RawCoding
@RawCoding 2 жыл бұрын
It's Rider, they have a trial you can try it out
@tonyschoborg
@tonyschoborg 2 жыл бұрын
@@obsidian741 Some things more objective and trivial. Looks, colors, etc. I really like the new cleaner UI. I originally switched when my VS was bugging out and I could not rename variables, it would just crash. spent better part of a day updating, reinstalling, restarting before I downloaded Rider. The biggest thing for me is spell check. And not only for comments and log messages, but even variable and class names. Also much better code analysis. there are a few things that Rider picked up on we could do better after we switched. A lot better options to refactor, as well. There are a lot of things that you will get from Resharper, if that is the route you go.
@ivandomaranskiy4694
@ivandomaranskiy4694 2 жыл бұрын
Thank you
@salehdanbous6783
@salehdanbous6783 2 жыл бұрын
I run into those usually when I have database update statements in my code.
@emreaka3965
@emreaka3965 6 ай бұрын
Thanks
@sorinvasiliu
@sorinvasiliu 2 жыл бұрын
awesome content, gg!
@cocoscacao6102
@cocoscacao6102 2 жыл бұрын
Everything that inherits Stream class next please... :)
@nutcracker6746
@nutcracker6746 2 жыл бұрын
Are you using Linux?
@RawCoding
@RawCoding 2 жыл бұрын
mac
@expertreviews1112
@expertreviews1112 2 жыл бұрын
awesome content! separates men from the boys
@BenChengnz
@BenChengnz 2 жыл бұрын
I wish I was as smart as you! 😂
@alperkaya8919
@alperkaya8919 Жыл бұрын
I respect your knowledge and expertise, but first example is totally wrong. var resource = 0; // Copy value 0 to resource variable var worker1 = resource; // Copy value 0 to worker1 variable var worker2 = resource; // Copy value 0 to worker2 variable worker1++; // Increase 0 by 1 and copy it to worker1, which is 1 worker2++; // Increase 0 by 1 and copy it to worker2, which is 1 resource = worker1; // Copy value of worker1 to resource, which is 1 resource = worker2; // Copy value of worker2 to resource, which overwrites the assignment above, which is 1 Console.WriteLine(resource); // Of course it will output 1, I really wonder what did you expect? I am ashamed of myself for explaining this concept as if it is not known already, but: C#, passes value types by copying their values, not their references. In this context, you cannot directly use resource variable. You can only copy it's current value. This is why you cannot get a race condition out of this.
@RawCoding
@RawCoding Жыл бұрын
The example is meant to illustrate what happens in parallel, but un a synchronous manner. So when I named things “worker” they are meant to represent 2 different processes grabbing the same resource at the same time, if they both read the same value and then write back the same value a race condition occurs.
@GebzNotJebz
@GebzNotJebz 5 ай бұрын
you've only explained half the topic, you've shown the problem but not the solution
@nekoill
@nekoill 11 ай бұрын
Lmao, that Java ripoff's compiler can't even detect race conditions?
Deadlocks in your .NET app
10:10
Raw Coding
Рет қаралды 7 М.
ASP.NET Core Kestrel Explained
15:58
Raw Coding
Рет қаралды 13 М.
Cheerleader Transformation That Left Everyone Speechless! #shorts
00:27
Fabiosa Best Lifehacks
Рет қаралды 16 МЛН
Гениальное изобретение из обычного стаканчика!
00:31
Лютая физика | Олимпиадная физика
Рет қаралды 4,8 МЛН
C# Channels Explained (System.Threading.Channels)
47:11
Raw Coding
Рет қаралды 37 М.
Settling the Biggest Await Async Debate in .NET
14:47
Nick Chapsas
Рет қаралды 148 М.
C# Async/Await/Task Explained (Deep Dive)
24:22
Raw Coding
Рет қаралды 422 М.
How to prevent race conditions in a reservation system
6:34
Web Dev Cody
Рет қаралды 24 М.
That's NOT How Async And Await Works in .NET!
12:25
Codewrinkles
Рет қаралды 27 М.
Semaphore Explained C#
12:24
Raw Coding
Рет қаралды 42 М.
.NET Core Garbage Collection
14:54
Coding Tutorials
Рет қаралды 25 М.
What you should know about Threads in .NET
14:52
Raw Coding
Рет қаралды 8 М.