Text version of the video
http://csharp-video-tutorials.blogspo...
Slides
http://csharp-video-tutorials.blogspo...
All C# Text Articles
http://csharp-video-tutorials.blogspo...
All C# Slides
http://csharp-video-tutorials.blogspo...
All Dot Net and SQL Server Tutorials
https://www.youtube.com/user/kudvenka...
In this video we will discuss,
1. What happens if shared resources are not protected from concurrent access in multithreaded program
2. How to protect shared resources from concurrent access
What happens if shared resources are not protected from concurrent access in multithreaded program
The output or behaviour of the program can become inconsistent if the shared resources are not protected from concurrent access in multithreaded program. Let us understand this with an example.
using System;
class Program
{ static int Total = 0; public static void Main() { AddOneMillion(); AddOneMillion(); AddOneMillion(); Console.WriteLine("Total = " + Total); } public static void AddOneMillion() { for (int i = 1; i [= 1000000; i++) { Total++; } }
}
The above program is a single-threaded program. In the Main() method, AddOneMillion() method is called 3 times, and it updates the Total field correctly as expected, and finally prints the correct total i.e 3000000.
Now, let's rewrite the program using multiple threads.
using System;
using System.Threading;
class Program
{ static int Total = 0; public static void Main() { Thread thread1 = new Thread(Program.AddOneMillion); Thread thread2 = new Thread(Program.AddOneMillion); Thread thread3 = new Thread(Program.AddOneMillion); thread1.Start(); thread2.Start(); thread3.Start(); thread1.Join(); thread2.Join(); thread3.Join(); Console.WriteLine("Total = " + Total); } public static void AddOneMillion() { for (int i = 1; i [= 1000000; i++) { Total++; } }
}
Every time we run the above program, we get a different output. The inconsistent output is because the Total field which is a shared resource is not protected from concurrent access by multiple threads. The operator ++ is not thread safe. There are several ways to fix this. Let's explore 2 of the options.
Using Interlocked.Increment() method: Modify AddOneMillion() method as shown below. The Interlocked.Increment() Method, increments a specified variable and stores the result, as an atomic operation
public static void AddOneMillion()
{ for (int i = 1; i [= 1000000; i++) { Interlocked.Increment(ref Total); }
}
The other option is to use a lock.
static object _lock = new object();
public static void AddOneMillion()
{ for (int i = 1; i [= 1000000; i++) { lock (_lock) { Total++; } }
}
Which option is better?
From a performance perspective using Interlocked class is better over using locking. Locking locks out all the other threads except a single thread to read and increment the Total variable. This will ensure that the Total variable is updated safely. The downside is that since all the other threads are locked out, there is a performance hit.
The Interlocked class can be used with addition/subtraction (increment, decrement, add, etc.) on and int or long field. The Interlocked class has methods for incrementing, decrementing, adding, and reading variables atomically.
The following code prints the time taken in ticks. 1 millisecond consists of 10000 ticks.
public static void Main()
{ Stopwatch stopwatch = Stopwatch.StartNew(); Thread thread1 = new Thread(Program.AddOneMillion); Thread thread2 = new Thread(Program.AddOneMillion); Thread thread3 = new Thread(Program.AddOneMillion); thread1.Start(); thread2.Start(); thread3.Start(); thread1.Join(); thread2.Join(); thread3.Join(); Console.WriteLine("Total = " + Total); stopwatch.Stop(); Console.WriteLine("Time Taken in Ticks = " + stopwatch.ElapsedTicks);
}
Please Note: You can use the TimeSpan object to find ticks per second, ticks per millisecond etc.
locked out of heaven lyrics Part 93 Protecting shared resources from concurrent access in multithreading | |
| 258 Likes | 258 Dislikes |
| 70,980 views views | 524K followers |
| Education | Upload TimePublished on 16 Mar 2014 |
Related keywords
asp.net core docker,wcf vs web api,ado.net core,sql server management studio,webkinz,webadvisor,multithreading vs multitasking,craigslist nj,wcf one piece,asp.net core 3,ajax players,weber grill parts,webtoon,tutorials by hugo,csharp corner,mvc design pattern,asp.net machine account,servers for minecraft,asp.net core dependency injection,ado.net tutorial,multithreading interview questions,locked away lyrics,services angular,locked out of heaven,ajax ontario,tutorial definition,tutorial on excel,asp.net mvc tutorial,multithreading powershell,asp.net cos'è,csharp assembly,multithreading vs multiprocessing python,multithreading python 3,tutorialspoint python,sql join,services briefcase,asp.net core web api,ajax jquery,wccftech,lock chat,craigslist ny,asp.net zero,csharp foreach,server memes,sql date format,services online,chase,multithreading programming,serverless architecture,server resume,wcf c#,server books,tutorialspoint javascript,mvcc connect,ado.net mysql,services technologies gps,ajax deadpool,server jobs,cvs,website,locker metalico,mvc tutorial,multithreading matlab,costco hours,lockheed martin,wcf service application,tutorialspoint spring,serverless,wcf soap,multithreading in c#,wcf cat,lockout,wcf test client,cool math games,services & training hse colombia sas,servicestack,citibank,asp.net core identity,sql union,ajax parking,sql database,asp.net core logging,mvconnect,asp.net guida,cunyfirst,wcf nba,csharp download,wcfi foundation,csharp online,wcf authentication,tutorials near me,http://asp.net,server 2019,ado.net descargar,web of dreams,chernobyl,serverminer,ajax cleaner,ado.net visual studio 2019,tutorial for windows 10,webassign,ado.net vs entity framework,ado.net visual studio 2017,csharp list,sql like,asp.net mvc,asp.net core tutorial,sqlite,wcfm,ajax roster,tutorial gateway,mvc architecture,http://ado.net,asp.net core mvc,ajax soccer,server hosting,wcfi,ajax dish soap,locke,locke biografia,capital one,server rack,tutorialspoint html,csharp interface,craigslist,webroot,tutorialspoint reactjs,ajax request,wcf dragon ball,asp.net core 2.2,tutorialspoint python 3,sql developer,webster,services transmission company sas,sql group by,asp.net core signalr,services manager,mvc framework,ajax paving,mvc near me,locking,tutorialspoint spring boot,multithreading vs single threading,mvc map,csharp online compiler,asp.net download,sql between,ado.net c# pdf,wcf 2019 nba,services tag dell,csharp switch,ado.net ventajas y desventajas,csharpstar,wcf tutorial,tutorialspoint,ajax meaning,csharp-video-tutorials.blogspot,tutorials dojo,tutorial mission gta online,csharp string format,central park 5,ado.net c#,asp.net core github,multithreading javascript,locker plastico,server status,ajax fc,server jobs nyc,asp.net core swagger,sql formatter,credit karma,services group,lockers bogota,locked away,server error in '/' application,multithreading example,services windows,asp.net core 3.0,sql injection,wcf ria services,tutorialspoint c#,calculator,ado.net entity data model,sql insert,tutorialspoint tableau,services google play apk,sqlyog,asp.net core 3 release date,sql server,server job description,tutorials by a,servicenow,webcam,mvc hours,multithreading vs multiprocessing,webmd symptom,csharp array,csharp enum,ajax call,asp.net core 2. guida completa per lo sviluppatore,asp.net core,server pro,server status ffxiv,webcrims,cheap flights,multithreading tutorial,multithreading in games,asp.net core hosting,services sas,tutorialspoint java,tutorialspoint java compiler,webmd,multithreading in java,csharp to json,college football,ado.net dataset,csharp dictionary,tutorial meaning,cnn,website builder,tutorial.mc-complex,tutorialspoint sql,asp.net web api,multithreading python,server side rendering,weber grills,sql server 2017,mvc nj,tutorialspoint spark,ado.net oracle,asp.net core download,csharp to vb.net,webster bank,webstaurant,tutorial systems,tutorial teacher,tutorialsystems,ajax post,services fms publish announcement,services.msc no abre,ajax jersey,csharp operator,asp.net core razor pages,server duties,asp.net core environment variables,locker,tutorial on variational autoencoders,csharp random,century 21,lockdown,services consultores,services consulting,locked,mvcsd,locked out of heaven letra,mvcsc,services.msc,ado.net pdf,asp.net core configuration,ajax marvel,tutorial synonym,sql update,asp.net tutorial,multithreading in unity,mvc medical,ado.net entity data model visual studio 2019,wcfs international curriculum,mvc2,locked up,multithreading arduino,ado.net entity data model visual studio 2017,chase online,wcf api,costco,server jobs near me,webex,sql meaning,locked away letra español,sql tutorial,sql commands,ado.net entity framework,ajax tavern,tutorialsteacher,ajax javascript,lockheed,services desk,multithreading concepts,ajax greek,csharp tutorial,mvc pattern,ado.net sql server,ado.net connection,asp.net identity,mvcu,asp.net core middleware,wcf web service,multithreading interview questions c#,mvc webadvisor,tutorial music,web store,mvcc,webmail,mvci,mvctc,
Không có nhận xét nào:
Đăng nhận xét