├── .gitignore ├── CADeadLock ├── CADeadLock.csproj └── Program.cs ├── CAMultithreading ├── CAMultithreading.csproj └── Program.cs ├── CAProcessAndThread ├── CAProcessAndThread.csproj └── Program.cs ├── CARaceCondition ├── CARaceCondition.csproj └── Program.cs ├── CASequential ├── CASequential.csproj └── Program.cs ├── CAThreadPool ├── CAThreadPool.csproj └── Program.cs ├── LICENSE └── MultiThreading.sln /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | ## 4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore 5 | 6 | # User-specific files 7 | *.rsuser 8 | *.suo 9 | *.user 10 | *.userosscache 11 | *.sln.docstates 12 | 13 | # User-specific files (MonoDevelop/Xamarin Studio) 14 | *.userprefs 15 | 16 | # Mono auto generated files 17 | mono_crash.* 18 | 19 | # Build results 20 | [Dd]ebug/ 21 | [Dd]ebugPublic/ 22 | [Rr]elease/ 23 | [Rr]eleases/ 24 | x64/ 25 | x86/ 26 | [Ww][Ii][Nn]32/ 27 | [Aa][Rr][Mm]/ 28 | [Aa][Rr][Mm]64/ 29 | bld/ 30 | [Bb]in/ 31 | [Oo]bj/ 32 | [Ll]og/ 33 | [Ll]ogs/ 34 | 35 | # Visual Studio 2015/2017 cache/options directory 36 | .vs/ 37 | # Uncomment if you have tasks that create the project's static files in wwwroot 38 | #wwwroot/ 39 | 40 | # Visual Studio 2017 auto generated files 41 | Generated\ Files/ 42 | 43 | # MSTest test Results 44 | [Tt]est[Rr]esult*/ 45 | [Bb]uild[Ll]og.* 46 | 47 | # NUnit 48 | *.VisualState.xml 49 | TestResult.xml 50 | nunit-*.xml 51 | 52 | # Build Results of an ATL Project 53 | [Dd]ebugPS/ 54 | [Rr]eleasePS/ 55 | dlldata.c 56 | 57 | # Benchmark Results 58 | BenchmarkDotNet.Artifacts/ 59 | 60 | # .NET Core 61 | project.lock.json 62 | project.fragment.lock.json 63 | artifacts/ 64 | 65 | # Tye 66 | .tye/ 67 | 68 | # ASP.NET Scaffolding 69 | ScaffoldingReadMe.txt 70 | 71 | # StyleCop 72 | StyleCopReport.xml 73 | 74 | # Files built by Visual Studio 75 | *_i.c 76 | *_p.c 77 | *_h.h 78 | *.ilk 79 | *.meta 80 | *.obj 81 | *.iobj 82 | *.pch 83 | *.pdb 84 | *.ipdb 85 | *.pgc 86 | *.pgd 87 | *.rsp 88 | *.sbr 89 | *.tlb 90 | *.tli 91 | *.tlh 92 | *.tmp 93 | *.tmp_proj 94 | *_wpftmp.csproj 95 | *.log 96 | *.vspscc 97 | *.vssscc 98 | .builds 99 | *.pidb 100 | *.svclog 101 | *.scc 102 | 103 | # Chutzpah Test files 104 | _Chutzpah* 105 | 106 | # Visual C++ cache files 107 | ipch/ 108 | *.aps 109 | *.ncb 110 | *.opendb 111 | *.opensdf 112 | *.sdf 113 | *.cachefile 114 | *.VC.db 115 | *.VC.VC.opendb 116 | 117 | # Visual Studio profiler 118 | *.psess 119 | *.vsp 120 | *.vspx 121 | *.sap 122 | 123 | # Visual Studio Trace Files 124 | *.e2e 125 | 126 | # TFS 2012 Local Workspace 127 | $tf/ 128 | 129 | # Guidance Automation Toolkit 130 | *.gpState 131 | 132 | # ReSharper is a .NET coding add-in 133 | _ReSharper*/ 134 | *.[Rr]e[Ss]harper 135 | *.DotSettings.user 136 | 137 | # TeamCity is a build add-in 138 | _TeamCity* 139 | 140 | # DotCover is a Code Coverage Tool 141 | *.dotCover 142 | 143 | # AxoCover is a Code Coverage Tool 144 | .axoCover/* 145 | !.axoCover/settings.json 146 | 147 | # Coverlet is a free, cross platform Code Coverage Tool 148 | coverage*.json 149 | coverage*.xml 150 | coverage*.info 151 | 152 | # Visual Studio code coverage results 153 | *.coverage 154 | *.coveragexml 155 | 156 | # NCrunch 157 | _NCrunch_* 158 | .*crunch*.local.xml 159 | nCrunchTemp_* 160 | 161 | # MightyMoose 162 | *.mm.* 163 | AutoTest.Net/ 164 | 165 | # Web workbench (sass) 166 | .sass-cache/ 167 | 168 | # Installshield output folder 169 | [Ee]xpress/ 170 | 171 | # DocProject is a documentation generator add-in 172 | DocProject/buildhelp/ 173 | DocProject/Help/*.HxT 174 | DocProject/Help/*.HxC 175 | DocProject/Help/*.hhc 176 | DocProject/Help/*.hhk 177 | DocProject/Help/*.hhp 178 | DocProject/Help/Html2 179 | DocProject/Help/html 180 | 181 | # Click-Once directory 182 | publish/ 183 | 184 | # Publish Web Output 185 | *.[Pp]ublish.xml 186 | *.azurePubxml 187 | # Note: Comment the next line if you want to checkin your web deploy settings, 188 | # but database connection strings (with potential passwords) will be unencrypted 189 | *.pubxml 190 | *.publishproj 191 | 192 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 193 | # checkin your Azure Web App publish settings, but sensitive information contained 194 | # in these scripts will be unencrypted 195 | PublishScripts/ 196 | 197 | # NuGet Packages 198 | *.nupkg 199 | # NuGet Symbol Packages 200 | *.snupkg 201 | # The packages folder can be ignored because of Package Restore 202 | **/[Pp]ackages/* 203 | # except build/, which is used as an MSBuild target. 204 | !**/[Pp]ackages/build/ 205 | # Uncomment if necessary however generally it will be regenerated when needed 206 | #!**/[Pp]ackages/repositories.config 207 | # NuGet v3's project.json files produces more ignorable files 208 | *.nuget.props 209 | *.nuget.targets 210 | 211 | # Microsoft Azure Build Output 212 | csx/ 213 | *.build.csdef 214 | 215 | # Microsoft Azure Emulator 216 | ecf/ 217 | rcf/ 218 | 219 | # Windows Store app package directories and files 220 | AppPackages/ 221 | BundleArtifacts/ 222 | Package.StoreAssociation.xml 223 | _pkginfo.txt 224 | *.appx 225 | *.appxbundle 226 | *.appxupload 227 | 228 | # Visual Studio cache files 229 | # files ending in .cache can be ignored 230 | *.[Cc]ache 231 | # but keep track of directories ending in .cache 232 | !?*.[Cc]ache/ 233 | 234 | # Others 235 | ClientBin/ 236 | ~$* 237 | *~ 238 | *.dbmdl 239 | *.dbproj.schemaview 240 | *.jfm 241 | *.pfx 242 | *.publishsettings 243 | orleans.codegen.cs 244 | 245 | # Including strong name files can present a security risk 246 | # (https://github.com/github/gitignore/pull/2483#issue-259490424) 247 | #*.snk 248 | 249 | # Since there are multiple workflows, uncomment next line to ignore bower_components 250 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 251 | #bower_components/ 252 | 253 | # RIA/Silverlight projects 254 | Generated_Code/ 255 | 256 | # Backup & report files from converting an old project file 257 | # to a newer Visual Studio version. Backup files are not needed, 258 | # because we have git ;-) 259 | _UpgradeReport_Files/ 260 | Backup*/ 261 | UpgradeLog*.XML 262 | UpgradeLog*.htm 263 | ServiceFabricBackup/ 264 | *.rptproj.bak 265 | 266 | # SQL Server files 267 | *.mdf 268 | *.ldf 269 | *.ndf 270 | 271 | # Business Intelligence projects 272 | *.rdl.data 273 | *.bim.layout 274 | *.bim_*.settings 275 | *.rptproj.rsuser 276 | *- [Bb]ackup.rdl 277 | *- [Bb]ackup ([0-9]).rdl 278 | *- [Bb]ackup ([0-9][0-9]).rdl 279 | 280 | # Microsoft Fakes 281 | FakesAssemblies/ 282 | 283 | # GhostDoc plugin setting file 284 | *.GhostDoc.xml 285 | 286 | # Node.js Tools for Visual Studio 287 | .ntvs_analysis.dat 288 | node_modules/ 289 | 290 | # Visual Studio 6 build log 291 | *.plg 292 | 293 | # Visual Studio 6 workspace options file 294 | *.opt 295 | 296 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 297 | *.vbw 298 | 299 | # Visual Studio LightSwitch build output 300 | **/*.HTMLClient/GeneratedArtifacts 301 | **/*.DesktopClient/GeneratedArtifacts 302 | **/*.DesktopClient/ModelManifest.xml 303 | **/*.Server/GeneratedArtifacts 304 | **/*.Server/ModelManifest.xml 305 | _Pvt_Extensions 306 | 307 | # Paket dependency manager 308 | .paket/paket.exe 309 | paket-files/ 310 | 311 | # FAKE - F# Make 312 | .fake/ 313 | 314 | # CodeRush personal settings 315 | .cr/personal 316 | 317 | # Python Tools for Visual Studio (PTVS) 318 | __pycache__/ 319 | *.pyc 320 | 321 | # Cake - Uncomment if you are using it 322 | # tools/** 323 | # !tools/packages.config 324 | 325 | # Tabs Studio 326 | *.tss 327 | 328 | # Telerik's JustMock configuration file 329 | *.jmconfig 330 | 331 | # BizTalk build output 332 | *.btp.cs 333 | *.btm.cs 334 | *.odx.cs 335 | *.xsd.cs 336 | 337 | # OpenCover UI analysis results 338 | OpenCover/ 339 | 340 | # Azure Stream Analytics local run output 341 | ASALocalRun/ 342 | 343 | # MSBuild Binary and Structured Log 344 | *.binlog 345 | 346 | # NVidia Nsight GPU debugger configuration file 347 | *.nvuser 348 | 349 | # MFractors (Xamarin productivity tool) working folder 350 | .mfractor/ 351 | 352 | # Local History for Visual Studio 353 | .localhistory/ 354 | 355 | # BeatPulse healthcheck temp database 356 | healthchecksdb 357 | 358 | # Backup folder for Package Reference Convert tool in Visual Studio 2017 359 | MigrationBackup/ 360 | 361 | # Ionide (cross platform F# VS Code tools) working folder 362 | .ionide/ 363 | 364 | # Fody - auto-generated XML schema 365 | FodyWeavers.xsd 366 | 367 | ## 368 | ## Visual studio for Mac 369 | ## 370 | 371 | 372 | # globs 373 | Makefile.in 374 | *.userprefs 375 | *.usertasks 376 | config.make 377 | config.status 378 | aclocal.m4 379 | install-sh 380 | autom4te.cache/ 381 | *.tar.gz 382 | tarballs/ 383 | test-results/ 384 | 385 | # Mac bundle stuff 386 | *.dmg 387 | *.app 388 | 389 | # content below from: https://github.com/github/gitignore/blob/master/Global/macOS.gitignore 390 | # General 391 | .DS_Store 392 | .AppleDouble 393 | .LSOverride 394 | 395 | # Icon must end with two \r 396 | Icon 397 | 398 | 399 | # Thumbnails 400 | ._* 401 | 402 | # Files that might appear in the root of a volume 403 | .DocumentRevisions-V100 404 | .fseventsd 405 | .Spotlight-V100 406 | .TemporaryItems 407 | .Trashes 408 | .VolumeIcon.icns 409 | .com.apple.timemachine.donotpresent 410 | 411 | # Directories potentially created on remote AFP share 412 | .AppleDB 413 | .AppleDesktop 414 | Network Trash Folder 415 | Temporary Items 416 | .apdisk 417 | 418 | # content below from: https://github.com/github/gitignore/blob/master/Global/Windows.gitignore 419 | # Windows thumbnail cache files 420 | Thumbs.db 421 | ehthumbs.db 422 | ehthumbs_vista.db 423 | 424 | # Dump file 425 | *.stackdump 426 | 427 | # Folder config file 428 | [Dd]esktop.ini 429 | 430 | # Recycle Bin used on file shares 431 | $RECYCLE.BIN/ 432 | 433 | # Windows Installer files 434 | *.cab 435 | *.msi 436 | *.msix 437 | *.msm 438 | *.msp 439 | 440 | # Windows shortcuts 441 | *.lnk 442 | 443 | # JetBrains Rider 444 | .idea/ 445 | *.sln.iml 446 | 447 | ## 448 | ## Visual Studio Code 449 | ## 450 | .vscode/* 451 | !.vscode/settings.json 452 | !.vscode/tasks.json 453 | !.vscode/launch.json 454 | !.vscode/extensions.json 455 | -------------------------------------------------------------------------------- /CADeadLock/CADeadLock.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net5.0 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /CADeadLock/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading; 3 | 4 | namespace CADeadLock 5 | { 6 | class Program 7 | { 8 | static void Main(string[] args) 9 | { 10 | var wallet1 = new Wallet(1, "Issam", 100); 11 | var wallet2 = new Wallet(2, "Reem", 50); 12 | Console.WriteLine("\n Before Transaction"); 13 | Console.WriteLine("\n---------------------"); 14 | Console.Write(wallet1 + ", "); Console.Write(wallet2); Console.WriteLine(); 15 | Console.WriteLine("\n After Transaction"); 16 | Console.WriteLine("\n---------------------"); 17 | 18 | var transferManager1 = new TransferManager(wallet1, wallet2, 50); 19 | var transferManager2 = new TransferManager(wallet2, wallet1, 30); 20 | 21 | var t1 = new Thread(transferManager1.Transfer); 22 | t1.Name = "T1"; 23 | var t2 = new Thread(transferManager2.Transfer); 24 | t2.Name = "T2"; 25 | 26 | t1.Start(); 27 | t2.Start(); 28 | 29 | t1.Join(); 30 | t2.Join(); 31 | 32 | Console.Write(wallet1 + ", "); Console.Write(wallet2); Console.WriteLine(); 33 | Console.ReadKey(); 34 | } 35 | } 36 | 37 | class Wallet 38 | { 39 | private readonly object bitcoinsLock = new object(); 40 | public Wallet(int id, string name, int bitcoins) 41 | { 42 | Id = id; 43 | Name = name; 44 | Bitcoins = bitcoins; 45 | } 46 | 47 | public int Id { get; private set; } 48 | public string Name { get; private set; } 49 | public int Bitcoins { get; private set; } 50 | 51 | 52 | public void Debit(int amount) 53 | { 54 | lock (bitcoinsLock) 55 | { 56 | if (Bitcoins >= amount) 57 | { 58 | Thread.Sleep(1000); 59 | 60 | Bitcoins -= amount; 61 | } 62 | } 63 | } 64 | 65 | public void Credit(int amount) 66 | { 67 | Thread.Sleep(1000); 68 | Bitcoins += amount; 69 | } 70 | 71 | 72 | public override string ToString() 73 | { 74 | return $"[{Name} -> {Bitcoins} Bitcoins]"; 75 | } 76 | } 77 | 78 | class TransferManager 79 | { 80 | private Wallet from; 81 | private Wallet to; 82 | 83 | private int amountToTransfer; 84 | 85 | public TransferManager(Wallet from, Wallet to, int amountToTransfer) 86 | { 87 | this.from = from; 88 | this.to = to; 89 | this.amountToTransfer = amountToTransfer; 90 | } 91 | 92 | public void Transfer() 93 | { 94 | var lock1 = from.Id < to.Id ? from : to; 95 | var lock2 = from.Id < to.Id ? to : from; 96 | 97 | Console.WriteLine($"{Thread.CurrentThread.Name} trying to lock ... {from}"); 98 | lock (lock1) 99 | { 100 | Console.WriteLine($"{Thread.CurrentThread.Name} lock acquired ... {from}"); 101 | Thread.Sleep(1000); 102 | Console.WriteLine($"{Thread.CurrentThread.Name} trying to lock ... {to}"); 103 | 104 | lock (lock2) 105 | { 106 | from.Debit(amountToTransfer); 107 | to.Credit(amountToTransfer); 108 | } 109 | } 110 | } 111 | } 112 | } 113 | -------------------------------------------------------------------------------- /CAMultithreading/CAMultithreading.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net5.0 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /CAMultithreading/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading; 3 | 4 | namespace CAMultithreading 5 | { 6 | class Program 7 | { 8 | static void Main(string[] args) 9 | { 10 | Thread.CurrentThread.Name = "Main Thread"; 11 | Console.WriteLine(Thread.CurrentThread.Name); 12 | //Console.WriteLine($"Background Thread: {Thread.CurrentThread.IsBackground}"); 13 | var wallet = new Wallet("Issam", 80); 14 | 15 | Thread t1 = new Thread(wallet.RunRandomTransactions); 16 | t1.Name = "T1"; 17 | //Console.WriteLine($"T1 Background Thread: {t1.IsBackground}"); 18 | Console.WriteLine($"after declaration {t1.Name} state is: {t1.ThreadState}"); 19 | 20 | t1.Start(); 21 | Console.WriteLine($"after start() {t1.Name} state is: {t1.ThreadState}"); 22 | t1.Join(); 23 | 24 | Thread t2 = new Thread(new ThreadStart(wallet.RunRandomTransactions)); 25 | t2.Name = "T2"; 26 | t2.Start(); 27 | 28 | Console.WriteLine($"after start {t1.Name} state is: {t1.ThreadState}"); 29 | 30 | 31 | 32 | Console.ReadKey(); 33 | } 34 | } 35 | 36 | class Wallet 37 | { 38 | public Wallet(string name, int bitcoins) 39 | { 40 | Name = name; 41 | Bitcoins = bitcoins; 42 | } 43 | 44 | public string Name { get; private set; } 45 | public int Bitcoins { get; private set; } 46 | 47 | 48 | public void Debit(int amount) 49 | { 50 | Thread.Sleep(1000); 51 | Bitcoins -= amount; 52 | Console.WriteLine( 53 | $"[Thread: {Thread.CurrentThread.ManagedThreadId}-{Thread.CurrentThread.Name} " + 54 | $", Processor Id: {Thread.GetCurrentProcessorId()}] -{amount}"); 55 | } 56 | 57 | public void Credit(int amount) 58 | { 59 | Thread.Sleep(1000); 60 | Bitcoins += amount; 61 | Console.WriteLine( 62 | $"[Thread: {Thread.CurrentThread.ManagedThreadId}-{Thread.CurrentThread.Name} " + 63 | $", Processor Id: {Thread.GetCurrentProcessorId()}] +{amount}"); 64 | } 65 | 66 | public void RunRandomTransactions() 67 | { 68 | int[] amounts = { 10, 20, 30, -20, 10, -10, 30, -10, 40, -20 }; // 80 69 | 70 | foreach (var amount in amounts) 71 | { 72 | var absValue = Math.Abs(amount); 73 | if (amount < 0) 74 | Debit(absValue); 75 | else 76 | Credit(absValue); 77 | 78 | } 79 | } 80 | 81 | public override string ToString() 82 | { 83 | return $"[{Name} -> {Bitcoins} Bitcoins]"; 84 | } 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /CAProcessAndThread/CAProcessAndThread.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net5.0 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /CAProcessAndThread/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Diagnostics; 3 | using System.Threading; 4 | 5 | namespace CAProcessAndThread 6 | { 7 | class Program 8 | { 9 | static void Main(string[] args) 10 | { 11 | Console.WriteLine($"Process Id: {Process.GetCurrentProcess().Id}"); 12 | Console.WriteLine($"Thread Id: {Thread.CurrentThread.ManagedThreadId}"); 13 | Console.WriteLine($"Processor Id: {Thread.GetCurrentProcessorId()}"); 14 | Console.ReadKey(); 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /CARaceCondition/CARaceCondition.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net5.0 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /CARaceCondition/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading; 3 | 4 | namespace CARaceCondition 5 | { 6 | class Program 7 | { 8 | static void Main(string[] args) 9 | { 10 | var wallet = new Wallet("Issam", 50); 11 | 12 | //wallet.Debit(40); 13 | //wallet.Debit(30); // 10 14 | 15 | 16 | var t1 = new Thread(() => wallet.Debit(40)); 17 | var t2 = new Thread(() => wallet.Debit(30)); 18 | 19 | t1.Start(); 20 | t2.Start(); 21 | 22 | t1.Join(); 23 | t2.Join(); 24 | 25 | Console.WriteLine(wallet); 26 | Console.ReadKey(); 27 | } 28 | } 29 | class Wallet 30 | { 31 | private readonly object bitcoinsLock = new object(); 32 | public Wallet(string name, int bitcoins) 33 | { 34 | Name = name; 35 | Bitcoins = bitcoins; 36 | } 37 | 38 | public string Name { get; private set; } 39 | public int Bitcoins { get; private set; } 40 | 41 | 42 | public void Debit(int amount) 43 | { 44 | lock (bitcoinsLock) 45 | { 46 | if (Bitcoins >= amount) 47 | { 48 | Thread.Sleep(1000); 49 | 50 | Bitcoins -= amount; 51 | } 52 | } 53 | } 54 | 55 | public void Credit(int amount) 56 | { 57 | Thread.Sleep(1000); 58 | Bitcoins += amount; 59 | } 60 | 61 | 62 | public override string ToString() 63 | { 64 | return $"[{Name} -> {Bitcoins} Bitcoins]"; 65 | } 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /CASequential/CASequential.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net5.0 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /CASequential/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading; 3 | 4 | namespace CASequential 5 | { 6 | class Program 7 | { 8 | static void Main(string[] args) 9 | { 10 | var wallet = new Wallet("Issam", 80); 11 | 12 | wallet.RunRandomTransactions(); 13 | Console.WriteLine("----------------"); 14 | Console.WriteLine($"{wallet}\n"); 15 | 16 | wallet.RunRandomTransactions(); 17 | Console.WriteLine("----------------"); 18 | Console.WriteLine($"{wallet}\n"); 19 | 20 | Console.ReadKey(); 21 | } 22 | } 23 | 24 | class Wallet 25 | { 26 | public Wallet(string name, int bitcoins) 27 | { 28 | Name = name; 29 | Bitcoins = bitcoins; 30 | } 31 | 32 | public string Name { get; private set; } 33 | public int Bitcoins { get; private set; } 34 | 35 | 36 | public void Debit(int amount) 37 | { 38 | Bitcoins -= amount; 39 | } 40 | 41 | public void Credit(int amount) 42 | { 43 | Bitcoins += amount; 44 | } 45 | 46 | public void RunRandomTransactions() 47 | { 48 | int[] amounts = { 10, 20, 30, -20, 10, -10, 30, -10, 40, -20 }; // 80 49 | 50 | foreach (var amount in amounts) 51 | { 52 | var absValue = Math.Abs(amount); 53 | if(amount < 0) 54 | Debit(absValue); 55 | else 56 | Credit(absValue); 57 | Console.WriteLine($"[Thread: {Thread.CurrentThread.ManagedThreadId}" + 58 | $", Processor Id: {Thread.GetCurrentProcessorId()}] {amount}"); 59 | } 60 | } 61 | 62 | public override string ToString() 63 | { 64 | return $"[{Name} -> {Bitcoins} Bitcoins]"; 65 | } 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /CAThreadPool/CAThreadPool.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net5.0 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /CAThreadPool/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading; 3 | using System.Threading.Tasks; 4 | 5 | namespace CAThreadPool 6 | { 7 | class Program 8 | { 9 | static void Main(string[] args) 10 | { 11 | Console.WriteLine("Using ThreadPool"); 12 | 13 | //1 14 | ThreadPool.QueueUserWorkItem(new WaitCallback(Print)); 15 | 16 | Console.WriteLine("Using Task"); 17 | //2 18 | Task.Run(Print); 19 | 20 | 21 | var employee = new Employee { Rate = 10, TotalHours = 40 }; 22 | 23 | ThreadPool.QueueUserWorkItem(new WaitCallback(CalculateSalary), employee); 24 | 25 | Console.WriteLine(employee.TotalSalary); 26 | Console.ReadKey(); 27 | } 28 | 29 | private static void CalculateSalary(object employee) 30 | { 31 | var emp = employee as Employee; 32 | if (employee is null) 33 | return; 34 | emp.TotalSalary = emp.TotalHours * emp.Rate; 35 | Console.WriteLine(emp.TotalSalary.ToString("C")); 36 | } 37 | 38 | private static void Print() 39 | { 40 | Console.WriteLine($"Thread Id: {Thread.CurrentThread.ManagedThreadId}, Thread Name: {Thread.CurrentThread.Name}"); 41 | Console.WriteLine($"Is Pooled thread: {Thread.CurrentThread.IsThreadPoolThread}"); 42 | Console.WriteLine($"Background: {Thread.CurrentThread.IsBackground}"); 43 | for (int i = 0; i < 10; i++) 44 | { 45 | Console.WriteLine($"Cycle {i + 1}"); 46 | } 47 | } 48 | 49 | private static void Print(object state) 50 | { 51 | Console.WriteLine($"Thread Id: {Thread.CurrentThread.ManagedThreadId}, Thread Name: {Thread.CurrentThread.Name}"); 52 | Console.WriteLine($"Is Pooled thread: {Thread.CurrentThread.IsThreadPoolThread}"); 53 | Console.WriteLine($"Background: {Thread.CurrentThread.IsBackground}"); 54 | for (int i = 0; i < 10; i++) 55 | { 56 | Console.WriteLine($"Cycle {i+1}"); 57 | } 58 | } 59 | } 60 | 61 | class Employee 62 | { 63 | public decimal TotalHours { get; set; } 64 | public decimal Rate { get; set; } 65 | 66 | public decimal TotalSalary { get; set; } 67 | 68 | } 69 | 70 | } 71 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Metigator 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /MultiThreading.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 16 4 | VisualStudioVersion = 16.0.31424.327 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CAProcessAndThread", "CAProcessAndThread\CAProcessAndThread.csproj", "{22AFA9D0-E5D5-4239-B758-EA310EC5003C}" 7 | EndProject 8 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CASequential", "CASequential\CASequential.csproj", "{43A9094D-6B94-49C1-9F95-CA8D05F4D485}" 9 | EndProject 10 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CAMultithreading", "CAMultithreading\CAMultithreading.csproj", "{5C19D05B-AD51-49AB-BC74-1946A5B1973E}" 11 | EndProject 12 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CARaceCondition", "CARaceCondition\CARaceCondition.csproj", "{A4F1CDE0-B1C7-4152-AAD0-48E3BD742C9E}" 13 | EndProject 14 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CADeadLock", "CADeadLock\CADeadLock.csproj", "{E69C42B0-6198-4A0B-A01D-6D8C75F68BAD}" 15 | EndProject 16 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CAThreadPool", "CAThreadPool\CAThreadPool.csproj", "{EACA388C-D33F-480B-A165-B75D51533246}" 17 | EndProject 18 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CATasks", "CATasks\CATasks.csproj", "{B2D69F14-810C-403E-9FD8-0BD7F866EEF9}" 19 | EndProject 20 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CATaskReturnValue", "CATaskReturnValue\CATaskReturnValue.csproj", "{47EF34BF-660C-478F-80E9-247182D5F204}" 21 | EndProject 22 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CATaskContinuation", "CATaskContinuation\CATaskContinuation.csproj", "{BDD97FEB-AE4A-460D-AFEB-CEB6B7444AE2}" 23 | EndProject 24 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CAAsyncProgramming", "CAAsyncProgramming\CAAsyncProgramming.csproj", "{428B6600-8C4E-4979-B489-2392458FCE5E}" 25 | EndProject 26 | Global 27 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 28 | Debug|Any CPU = Debug|Any CPU 29 | Release|Any CPU = Release|Any CPU 30 | EndGlobalSection 31 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 32 | {22AFA9D0-E5D5-4239-B758-EA310EC5003C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 33 | {22AFA9D0-E5D5-4239-B758-EA310EC5003C}.Debug|Any CPU.Build.0 = Debug|Any CPU 34 | {22AFA9D0-E5D5-4239-B758-EA310EC5003C}.Release|Any CPU.ActiveCfg = Release|Any CPU 35 | {22AFA9D0-E5D5-4239-B758-EA310EC5003C}.Release|Any CPU.Build.0 = Release|Any CPU 36 | {43A9094D-6B94-49C1-9F95-CA8D05F4D485}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 37 | {43A9094D-6B94-49C1-9F95-CA8D05F4D485}.Debug|Any CPU.Build.0 = Debug|Any CPU 38 | {43A9094D-6B94-49C1-9F95-CA8D05F4D485}.Release|Any CPU.ActiveCfg = Release|Any CPU 39 | {43A9094D-6B94-49C1-9F95-CA8D05F4D485}.Release|Any CPU.Build.0 = Release|Any CPU 40 | {5C19D05B-AD51-49AB-BC74-1946A5B1973E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 41 | {5C19D05B-AD51-49AB-BC74-1946A5B1973E}.Debug|Any CPU.Build.0 = Debug|Any CPU 42 | {5C19D05B-AD51-49AB-BC74-1946A5B1973E}.Release|Any CPU.ActiveCfg = Release|Any CPU 43 | {5C19D05B-AD51-49AB-BC74-1946A5B1973E}.Release|Any CPU.Build.0 = Release|Any CPU 44 | {A4F1CDE0-B1C7-4152-AAD0-48E3BD742C9E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 45 | {A4F1CDE0-B1C7-4152-AAD0-48E3BD742C9E}.Debug|Any CPU.Build.0 = Debug|Any CPU 46 | {A4F1CDE0-B1C7-4152-AAD0-48E3BD742C9E}.Release|Any CPU.ActiveCfg = Release|Any CPU 47 | {A4F1CDE0-B1C7-4152-AAD0-48E3BD742C9E}.Release|Any CPU.Build.0 = Release|Any CPU 48 | {E69C42B0-6198-4A0B-A01D-6D8C75F68BAD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 49 | {E69C42B0-6198-4A0B-A01D-6D8C75F68BAD}.Debug|Any CPU.Build.0 = Debug|Any CPU 50 | {E69C42B0-6198-4A0B-A01D-6D8C75F68BAD}.Release|Any CPU.ActiveCfg = Release|Any CPU 51 | {E69C42B0-6198-4A0B-A01D-6D8C75F68BAD}.Release|Any CPU.Build.0 = Release|Any CPU 52 | {EACA388C-D33F-480B-A165-B75D51533246}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 53 | {EACA388C-D33F-480B-A165-B75D51533246}.Debug|Any CPU.Build.0 = Debug|Any CPU 54 | {EACA388C-D33F-480B-A165-B75D51533246}.Release|Any CPU.ActiveCfg = Release|Any CPU 55 | {EACA388C-D33F-480B-A165-B75D51533246}.Release|Any CPU.Build.0 = Release|Any CPU 56 | {B2D69F14-810C-403E-9FD8-0BD7F866EEF9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 57 | {B2D69F14-810C-403E-9FD8-0BD7F866EEF9}.Debug|Any CPU.Build.0 = Debug|Any CPU 58 | {B2D69F14-810C-403E-9FD8-0BD7F866EEF9}.Release|Any CPU.ActiveCfg = Release|Any CPU 59 | {B2D69F14-810C-403E-9FD8-0BD7F866EEF9}.Release|Any CPU.Build.0 = Release|Any CPU 60 | {47EF34BF-660C-478F-80E9-247182D5F204}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 61 | {47EF34BF-660C-478F-80E9-247182D5F204}.Debug|Any CPU.Build.0 = Debug|Any CPU 62 | {47EF34BF-660C-478F-80E9-247182D5F204}.Release|Any CPU.ActiveCfg = Release|Any CPU 63 | {47EF34BF-660C-478F-80E9-247182D5F204}.Release|Any CPU.Build.0 = Release|Any CPU 64 | {BDD97FEB-AE4A-460D-AFEB-CEB6B7444AE2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 65 | {BDD97FEB-AE4A-460D-AFEB-CEB6B7444AE2}.Debug|Any CPU.Build.0 = Debug|Any CPU 66 | {BDD97FEB-AE4A-460D-AFEB-CEB6B7444AE2}.Release|Any CPU.ActiveCfg = Release|Any CPU 67 | {BDD97FEB-AE4A-460D-AFEB-CEB6B7444AE2}.Release|Any CPU.Build.0 = Release|Any CPU 68 | {428B6600-8C4E-4979-B489-2392458FCE5E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 69 | {428B6600-8C4E-4979-B489-2392458FCE5E}.Debug|Any CPU.Build.0 = Debug|Any CPU 70 | {428B6600-8C4E-4979-B489-2392458FCE5E}.Release|Any CPU.ActiveCfg = Release|Any CPU 71 | {428B6600-8C4E-4979-B489-2392458FCE5E}.Release|Any CPU.Build.0 = Release|Any CPU 72 | EndGlobalSection 73 | GlobalSection(SolutionProperties) = preSolution 74 | HideSolutionNode = FALSE 75 | EndGlobalSection 76 | GlobalSection(ExtensibilityGlobals) = postSolution 77 | SolutionGuid = {E08BB2AF-F5E0-4C74-938C-E10A8F6DE846} 78 | EndGlobalSection 79 | EndGlobal 80 | --------------------------------------------------------------------------------