├── .gitattributes ├── .gitignore ├── ParallelDemo.sln ├── ParallelDemo ├── App.config ├── App.xaml ├── App.xaml.cs ├── AsyncDemo │ └── AsyncDemo.cs ├── BlockingLazy │ └── SimpleBlockingLazy.cs ├── BlockingQueue │ └── SimpleBlockingQueue.cs ├── MainWindow.xaml ├── MainWindow.xaml.cs ├── ParallelDemo.csproj ├── Properties │ ├── AssemblyInfo.cs │ ├── Resources.Designer.cs │ ├── Resources.resx │ ├── Settings.Designer.cs │ └── Settings.settings ├── ReadWriteLock │ ├── ReadWriteLockSlimEx.cs │ ├── SimpleReadWriteLock.cs │ └── TestRWLock.cs └── packages.config └── README.md /.gitattributes: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # Set default behavior to automatically normalize line endings. 3 | ############################################################################### 4 | * text=auto 5 | 6 | ############################################################################### 7 | # Set default behavior for command prompt diff. 8 | # 9 | # This is need for earlier builds of msysgit that does not have it on by 10 | # default for csharp files. 11 | # Note: This is only used by command line 12 | ############################################################################### 13 | #*.cs diff=csharp 14 | 15 | ############################################################################### 16 | # Set the merge driver for project and solution files 17 | # 18 | # Merging from the command prompt will add diff markers to the files if there 19 | # are conflicts (Merging from VS is not affected by the settings below, in VS 20 | # the diff markers are never inserted). Diff markers may cause the following 21 | # file extensions to fail to load in VS. An alternative would be to treat 22 | # these files as binary and thus will always conflict and require user 23 | # intervention with every merge. To do so, just uncomment the entries below 24 | ############################################################################### 25 | #*.sln merge=binary 26 | #*.csproj merge=binary 27 | #*.vbproj merge=binary 28 | #*.vcxproj merge=binary 29 | #*.vcproj merge=binary 30 | #*.dbproj merge=binary 31 | #*.fsproj merge=binary 32 | #*.lsproj merge=binary 33 | #*.wixproj merge=binary 34 | #*.modelproj merge=binary 35 | #*.sqlproj merge=binary 36 | #*.wwaproj merge=binary 37 | 38 | ############################################################################### 39 | # behavior for image files 40 | # 41 | # image files are treated as binary by default. 42 | ############################################################################### 43 | #*.jpg binary 44 | #*.png binary 45 | #*.gif binary 46 | 47 | ############################################################################### 48 | # diff behavior for common document formats 49 | # 50 | # Convert binary document formats to text before diffing them. This feature 51 | # is only available from the command line. Turn it on by uncommenting the 52 | # entries below. 53 | ############################################################################### 54 | #*.doc diff=astextplain 55 | #*.DOC diff=astextplain 56 | #*.docx diff=astextplain 57 | #*.DOCX diff=astextplain 58 | #*.dot diff=astextplain 59 | #*.DOT diff=astextplain 60 | #*.pdf diff=astextplain 61 | #*.PDF diff=astextplain 62 | #*.rtf diff=astextplain 63 | #*.RTF diff=astextplain 64 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | 4 | # User-specific files 5 | *.suo 6 | *.user 7 | *.sln.docstates 8 | 9 | # Build results 10 | [Dd]ebug/ 11 | [Dd]ebugPublic/ 12 | [Rr]elease/ 13 | x64/ 14 | build/ 15 | bld/ 16 | [Bb]in/ 17 | [Oo]bj/ 18 | 19 | # Roslyn cache directories 20 | *.ide/ 21 | 22 | # MSTest test Results 23 | [Tt]est[Rr]esult*/ 24 | [Bb]uild[Ll]og.* 25 | 26 | #NUNIT 27 | *.VisualState.xml 28 | TestResult.xml 29 | 30 | # Build Results of an ATL Project 31 | [Dd]ebugPS/ 32 | [Rr]eleasePS/ 33 | dlldata.c 34 | 35 | *_i.c 36 | *_p.c 37 | *_i.h 38 | *.ilk 39 | *.meta 40 | *.obj 41 | *.pch 42 | *.pdb 43 | *.pgc 44 | *.pgd 45 | *.rsp 46 | *.sbr 47 | *.tlb 48 | *.tli 49 | *.tlh 50 | *.tmp 51 | *.tmp_proj 52 | *.log 53 | *.vspscc 54 | *.vssscc 55 | .builds 56 | *.pidb 57 | *.svclog 58 | *.scc 59 | 60 | # Chutzpah Test files 61 | _Chutzpah* 62 | 63 | # Visual C++ cache files 64 | ipch/ 65 | *.aps 66 | *.ncb 67 | *.opensdf 68 | *.sdf 69 | *.cachefile 70 | 71 | # Visual Studio profiler 72 | *.psess 73 | *.vsp 74 | *.vspx 75 | 76 | # TFS 2012 Local Workspace 77 | $tf/ 78 | 79 | # Guidance Automation Toolkit 80 | *.gpState 81 | 82 | # ReSharper is a .NET coding add-in 83 | _ReSharper*/ 84 | *.[Rr]e[Ss]harper 85 | *.DotSettings.user 86 | 87 | # JustCode is a .NET coding addin-in 88 | .JustCode 89 | 90 | # TeamCity is a build add-in 91 | _TeamCity* 92 | 93 | # DotCover is a Code Coverage Tool 94 | *.dotCover 95 | 96 | # NCrunch 97 | _NCrunch_* 98 | .*crunch*.local.xml 99 | 100 | # MightyMoose 101 | *.mm.* 102 | AutoTest.Net/ 103 | 104 | # Web workbench (sass) 105 | .sass-cache/ 106 | 107 | # Installshield output folder 108 | [Ee]xpress/ 109 | 110 | # DocProject is a documentation generator add-in 111 | DocProject/buildhelp/ 112 | DocProject/Help/*.HxT 113 | DocProject/Help/*.HxC 114 | DocProject/Help/*.hhc 115 | DocProject/Help/*.hhk 116 | DocProject/Help/*.hhp 117 | DocProject/Help/Html2 118 | DocProject/Help/html 119 | 120 | # Click-Once directory 121 | publish/ 122 | 123 | # Publish Web Output 124 | *.[Pp]ublish.xml 125 | *.azurePubxml 126 | ## TODO: Comment the next line if you want to checkin your 127 | ## web deploy settings but do note that will include unencrypted 128 | ## passwords 129 | #*.pubxml 130 | 131 | # NuGet Packages Directory 132 | packages/* 133 | ## TODO: If the tool you use requires repositories.config 134 | ## uncomment the next line 135 | #!packages/repositories.config 136 | 137 | # Enable "build/" folder in the NuGet Packages folder since 138 | # NuGet packages use it for MSBuild targets. 139 | # This line needs to be after the ignore of the build folder 140 | # (and the packages folder if the line above has been uncommented) 141 | !packages/build/ 142 | 143 | # Windows Azure Build Output 144 | csx/ 145 | *.build.csdef 146 | 147 | # Windows Store app package directory 148 | AppPackages/ 149 | 150 | # Others 151 | sql/ 152 | *.Cache 153 | ClientBin/ 154 | [Ss]tyle[Cc]op.* 155 | ~$* 156 | *~ 157 | *.dbmdl 158 | *.dbproj.schemaview 159 | *.pfx 160 | *.publishsettings 161 | node_modules/ 162 | 163 | # RIA/Silverlight projects 164 | Generated_Code/ 165 | 166 | # Backup & report files from converting an old project file 167 | # to a newer Visual Studio version. Backup files are not needed, 168 | # because we have git ;-) 169 | _UpgradeReport_Files/ 170 | Backup*/ 171 | UpgradeLog*.XML 172 | UpgradeLog*.htm 173 | 174 | # SQL Server files 175 | *.mdf 176 | *.ldf 177 | 178 | # Business Intelligence projects 179 | *.rdl.data 180 | *.bim.layout 181 | *.bim_*.settings 182 | 183 | # Microsoft Fakes 184 | FakesAssemblies/ 185 | 186 | # LightSwitch generated files 187 | GeneratedArtifacts/ 188 | _Pvt_Extensions/ 189 | ModelManifest.xml -------------------------------------------------------------------------------- /ParallelDemo.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 2013 4 | VisualStudioVersion = 12.0.31101.0 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ParallelDemo", "ParallelDemo\ParallelDemo.csproj", "{5BC69670-B51C-4E2B-A167-E8C2D6C07EB0}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|Any CPU = Debug|Any CPU 11 | Release|Any CPU = Release|Any CPU 12 | EndGlobalSection 13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 14 | {5BC69670-B51C-4E2B-A167-E8C2D6C07EB0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 15 | {5BC69670-B51C-4E2B-A167-E8C2D6C07EB0}.Debug|Any CPU.Build.0 = Debug|Any CPU 16 | {5BC69670-B51C-4E2B-A167-E8C2D6C07EB0}.Release|Any CPU.ActiveCfg = Release|Any CPU 17 | {5BC69670-B51C-4E2B-A167-E8C2D6C07EB0}.Release|Any CPU.Build.0 = Release|Any CPU 18 | EndGlobalSection 19 | GlobalSection(SolutionProperties) = preSolution 20 | HideSolutionNode = FALSE 21 | EndGlobalSection 22 | EndGlobal 23 | -------------------------------------------------------------------------------- /ParallelDemo/App.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /ParallelDemo/App.xaml: -------------------------------------------------------------------------------- 1 |  5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /ParallelDemo/App.xaml.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Configuration; 4 | using System.Data; 5 | using System.Linq; 6 | using System.Threading.Tasks; 7 | using System.Windows; 8 | 9 | namespace ParallelDemo 10 | { 11 | /// 12 | /// Interaction logic for App.xaml 13 | /// 14 | public partial class App : Application 15 | { 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /ParallelDemo/AsyncDemo/AsyncDemo.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading; 3 | using System.Threading.Tasks; 4 | using NUnit.Framework; 5 | // ReSharper disable LocalizableElement 6 | 7 | namespace ParallelDemo.AsyncDemo 8 | { 9 | public class AsyncDemo 10 | { 11 | int A() 12 | { 13 | Console.WriteLine("A: {0}" , Thread.CurrentThread.ManagedThreadId); 14 | return 1; 15 | } 16 | 17 | Task B(int p) 18 | { 19 | return Task.Run(() => 20 | { 21 | Console.WriteLine("B: {0}", Thread.CurrentThread.ManagedThreadId); 22 | return p + 2; 23 | }); 24 | } 25 | 26 | // async Task B(int p) 27 | // { 28 | // return p + 2; 29 | // } 30 | 31 | 32 | int C(int p) 33 | { 34 | Console.WriteLine("C: {0}", Thread.CurrentThread.ManagedThreadId); 35 | return p + 3; 36 | } 37 | 38 | 39 | async Task AsyncMethod() 40 | { 41 | int a = A(); 42 | int b = await B(a).ConfigureAwait(false); 43 | return C(b); 44 | } 45 | 46 | 47 | 48 | 49 | [Test] 50 | public void TestAsync() 51 | { 52 | Console.WriteLine("Started: {0}", Thread.CurrentThread.ManagedThreadId); 53 | var task = EmulatedAsyncMethod(); 54 | Assert.AreEqual(6, task.Result); 55 | } 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | Task EmulatedAsyncMethod() 66 | { 67 | int a = A(); 68 | return Task.Factory.StartNew(() => 69 | { 70 | return B(a).ContinueWith(_ => C(_.Result), TaskContinuationOptions.AttachedToParent); 71 | }).ContinueWith(task => 72 | { 73 | Assert.True(task.IsCompleted); 74 | Assert.True(task.Result.IsCompleted); 75 | return task.Result.Result; 76 | }); 77 | } 78 | 79 | } 80 | } -------------------------------------------------------------------------------- /ParallelDemo/BlockingLazy/SimpleBlockingLazy.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Diagnostics; 3 | using System.Threading; 4 | 5 | namespace ParallelDemo.BlockingLazy 6 | { 7 | public class SimpleBlockingLazy where T:class 8 | { 9 | private readonly object myLock = new object(); 10 | private readonly Func myProducer; 11 | 12 | private T myValue; 13 | 14 | public SimpleBlockingLazy(Func producer) 15 | { 16 | myProducer = producer; 17 | } 18 | 19 | public T Value 20 | { 21 | get 22 | { 23 | var res = myValue; 24 | if (res != null) return res; 25 | 26 | lock (myLock) 27 | { 28 | if (myValue != null) return myValue; 29 | 30 | res = myProducer(); 31 | Thread.MemoryBarrier(); 32 | myValue = res; 33 | 34 | Debug.Assert(myValue != null); 35 | return myValue; 36 | } 37 | } 38 | } 39 | } 40 | } -------------------------------------------------------------------------------- /ParallelDemo/BlockingQueue/SimpleBlockingQueue.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections; 3 | using System.Collections.Concurrent; 4 | using System.Collections.Generic; 5 | using System.Diagnostics; 6 | using System.Threading; 7 | 8 | namespace ParallelDemo.BlockingQueue 9 | { 10 | /// 11 | /// Concurrent blocking queue with predefined size. All operations are thread safe. Operations like ToArray or GetEnumerator 12 | /// takes queue snapshot at some moment. 13 | /// 14 | /// 15 | 16 | public class SimpleBlockingQueue : IProducerConsumerCollection where T : class 17 | { 18 | private const long BlockingOperationInProgressMarker = -1; 19 | 20 | private readonly T[] myQueue; 21 | private readonly long myMask; 22 | private readonly int mySize; 23 | 24 | private long myHead; 25 | private long myTail; 26 | 27 | /// 28 | /// Constructs queue with predefined size. Queue size is defined logariphmically. Queue is not growing, nor shrinking. 29 | /// 30 | /// From 1 to 20. Real queue size will be 1<<. 31 | public SimpleBlockingQueue(int logMaxSize) 32 | { 33 | if (logMaxSize <= 1 || logMaxSize > 20) throw new ArgumentException("Bad logsize:" + logMaxSize); 34 | 35 | mySize = 1 << logMaxSize; 36 | myQueue = new T[mySize]; 37 | myMask = mySize - 1; 38 | } 39 | 40 | 41 | #region Nonblocking API 42 | 43 | /// 44 | /// Enqueues to the tail of the queue. Increases tail. 45 | /// 46 | /// 47 | /// 48 | public bool TryAdd(T t) 49 | { 50 | if (t == null) throw new ArgumentNullException("t"); 51 | 52 | while (true) 53 | { 54 | //prologue 55 | var head = Interlocked.Read(ref myHead); 56 | var tail = Interlocked.Read(ref myTail); 57 | // if (head == BlockingOperationInProgressMarker || tail == BlockingOperationInProgressMarker) 58 | // { 59 | // Thread.Yield(); 60 | // continue; 61 | // } 62 | 63 | //check 64 | if (tail - head >= mySize) return false; 65 | 66 | //set 67 | if (tail == Interlocked.CompareExchange(ref myTail, tail + 1, tail)) 68 | { 69 | var idx = tail & myMask; 70 | myQueue[idx] = t; 71 | return true; 72 | } 73 | 74 | Thread.Yield(); 75 | } 76 | } 77 | 78 | /// 79 | /// Dequeues from the head of the queue. Moves head. 80 | /// 81 | /// 82 | /// 83 | public bool TryDequeue(out T res) 84 | { 85 | while (true) 86 | { 87 | //prologue 88 | var head = Interlocked.Read(ref myHead); 89 | var tail = Interlocked.Read(ref myTail); 90 | if (head == BlockingOperationInProgressMarker || tail == BlockingOperationInProgressMarker) 91 | { 92 | Thread.Yield(); 93 | continue; 94 | } 95 | 96 | //check 97 | if (head >= tail) 98 | { 99 | res = null; 100 | return false; 101 | } 102 | 103 | //set 104 | var queue = myQueue; 105 | var idx = head & myMask; 106 | res = queue[idx]; 107 | if (res != null && head == Interlocked.CompareExchange(ref myHead, head + 1, head)) 108 | { 109 | Interlocked.CompareExchange(ref queue[idx], null, res); //if some producer already set this index with new value, don't clear; 110 | return true; 111 | } 112 | 113 | Thread.Yield(); 114 | } 115 | } 116 | 117 | /// 118 | /// Peeks from the head of the queue. Leaves head untouched. Sequential peeks will return same result if no other thread dequeues or clears simultaneously. 119 | /// 120 | /// 121 | /// 122 | public bool TryPeek(out T res) 123 | { 124 | while (true) 125 | { 126 | //prologue 127 | var head = Interlocked.Read(ref myHead); 128 | var tail = Interlocked.Read(ref myTail); 129 | if (head == BlockingOperationInProgressMarker || tail == BlockingOperationInProgressMarker) 130 | { 131 | Thread.Yield(); 132 | continue; 133 | } 134 | 135 | //check 136 | if (head >= tail) 137 | { 138 | res = null; 139 | return false; 140 | } 141 | 142 | //op 143 | var queue = myQueue; 144 | var idx = head & myMask; 145 | if ((res = queue[idx]) != null) return true; 146 | 147 | Thread.Yield(); 148 | } 149 | } 150 | 151 | 152 | /// 153 | /// Number of elements in queue snapshot. 154 | /// 155 | public int Count 156 | { 157 | get 158 | { 159 | while (true) 160 | { 161 | //prologue 162 | var head = Interlocked.Read(ref myHead); 163 | var tail = Interlocked.Read(ref myTail); 164 | if (head != BlockingOperationInProgressMarker && tail != BlockingOperationInProgressMarker) 165 | { 166 | return (int)(tail - head); 167 | } 168 | Thread.Yield(); 169 | } 170 | } 171 | } 172 | 173 | #endregion 174 | 175 | 176 | 177 | #region Blocking API 178 | 179 | public void CopyTo(T[] array, int index) 180 | { 181 | DoImmutableBlockingOp((head, tail) => 182 | { 183 | while (head < tail) array[index++] = myQueue[head++ & myMask]; 184 | }); 185 | } 186 | 187 | 188 | public T[] ToArray() 189 | { 190 | return DoImmutableBlockingOp((head, tail) => 191 | { 192 | var index = 0; 193 | var array = new T[tail - head]; 194 | while (head < tail) array[index++] = myQueue[head++ & myMask]; 195 | return array; 196 | }); 197 | } 198 | 199 | 200 | public void CopyTo(Array array, int index) 201 | { 202 | DoImmutableBlockingOp((head, tail) => 203 | { 204 | while (head < tail) array.SetValue(myQueue[head++ & myMask], index++); 205 | }); 206 | } 207 | 208 | public void Clear() 209 | { 210 | { 211 | while (true) 212 | { 213 | //prologue 214 | var head = Interlocked.Read(ref myHead); 215 | var tail = Interlocked.Read(ref myTail); 216 | if (head == BlockingOperationInProgressMarker || tail == BlockingOperationInProgressMarker) 217 | { 218 | Thread.Yield(); 219 | continue; 220 | } 221 | 222 | //set 223 | if (head == Interlocked.CompareExchange(ref myHead, BlockingOperationInProgressMarker, head)) 224 | { 225 | if (tail == Interlocked.CompareExchange(ref myTail, BlockingOperationInProgressMarker, tail)) 226 | { 227 | try 228 | { 229 | Array.Clear(myQueue, 0, mySize); 230 | } 231 | finally 232 | { 233 | var prevtail = Interlocked.Exchange(ref myTail, 0); 234 | var prevhead = Interlocked.Exchange(ref myHead, 0); 235 | Debug.Assert(prevtail == BlockingOperationInProgressMarker, "Invalid state: tail"); 236 | Debug.Assert(prevhead == BlockingOperationInProgressMarker, "Invalid state: head"); 237 | } 238 | return; 239 | } 240 | else 241 | { 242 | var prevhead = Interlocked.Exchange(ref myHead, head); 243 | Debug.Assert(prevhead == BlockingOperationInProgressMarker, "Invalid state: head"); 244 | } 245 | } 246 | 247 | 248 | //continue 249 | Thread.Yield(); 250 | } 251 | } 252 | } 253 | 254 | 255 | private void DoImmutableBlockingOp(Action op) 256 | { 257 | DoImmutableBlockingOp((head, tail) => { op(head, tail); return null; }); 258 | } 259 | 260 | private TParam DoImmutableBlockingOp(Func op) 261 | { 262 | while (true) 263 | { 264 | //prologue 265 | var head = Interlocked.Read(ref myHead); 266 | var tail = Interlocked.Read(ref myTail); 267 | if (head == BlockingOperationInProgressMarker || tail == BlockingOperationInProgressMarker) 268 | { 269 | Thread.Yield(); 270 | continue; 271 | } 272 | 273 | //set 274 | if (head == Interlocked.CompareExchange(ref myHead, BlockingOperationInProgressMarker, head)) 275 | { 276 | try 277 | { 278 | if (tail == Interlocked.CompareExchange(ref myTail, BlockingOperationInProgressMarker, tail)) 279 | { 280 | try 281 | { 282 | return op(head, tail); 283 | } 284 | finally 285 | { 286 | var prevtail = Interlocked.Exchange(ref myTail, tail); 287 | Debug.Assert(prevtail == BlockingOperationInProgressMarker, "Invalid state: tail"); 288 | } 289 | 290 | } 291 | } 292 | finally 293 | { 294 | var prevhead = Interlocked.Exchange(ref myHead, head); 295 | Debug.Assert(prevhead == BlockingOperationInProgressMarker, "Invalid state: head"); 296 | } 297 | } 298 | 299 | //continue 300 | Thread.Yield(); 301 | } 302 | } 303 | 304 | 305 | #endregion 306 | 307 | 308 | 309 | #region Other Collection API 310 | 311 | /// 312 | /// Not real SyncRoot, but you can use it for external synchronization 313 | /// 314 | public object SyncRoot 315 | { 316 | get { return myQueue; } 317 | } 318 | 319 | 320 | public bool IsSynchronized 321 | { 322 | get { return true; } 323 | } 324 | 325 | public bool IsEmpty 326 | { 327 | get { return Count == 0; } 328 | } 329 | 330 | public IEnumerator GetEnumerator() 331 | { 332 | return ((IList)ToArray()).GetEnumerator(); 333 | } 334 | 335 | IEnumerator IEnumerable.GetEnumerator() 336 | { 337 | return GetEnumerator(); 338 | } 339 | 340 | /// 341 | /// Same as . Implementation of 342 | /// 343 | /// 344 | /// 345 | public bool TryTake(out T res) 346 | { 347 | return TryDequeue(out res); 348 | } 349 | #endregion 350 | 351 | 352 | #region Simple API 353 | 354 | /// 355 | /// Same as but returns null returns false 356 | /// 357 | /// 358 | public T Dequeue() 359 | { 360 | T res; 361 | TryDequeue(out res); 362 | return res; 363 | } 364 | 365 | /// 366 | /// Same as but returns null when returns false 367 | /// 368 | /// 369 | public T Peek() 370 | { 371 | T res; 372 | TryPeek(out res); 373 | return res; 374 | } 375 | 376 | #endregion 377 | } 378 | } -------------------------------------------------------------------------------- /ParallelDemo/MainWindow.xaml: -------------------------------------------------------------------------------- 1 |  5 | 6 | #Primes before N(inclusively) 7 | 8 | N: 9 | 10 | 11 | 12 | 13 | Answer: 14 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /ParallelDemo/MainWindow.xaml.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | using System.Security.Permissions; 4 | using System.Threading; 5 | using System.Threading.Tasks; 6 | using System.Windows; 7 | using System.Windows.Input; 8 | using System.Windows.Threading; 9 | 10 | // ReSharper disable CoVariantArrayConversion 11 | 12 | namespace ParallelDemo 13 | { 14 | public partial class MainWindow 15 | { 16 | public MainWindow() 17 | { 18 | InitializeComponent(); 19 | } 20 | 21 | private void OnKeyDown(object sender, KeyEventArgs e) 22 | { 23 | if (e.Key == Key.Return) 24 | { 25 | Do(); 26 | } 27 | else if (e.Key == Key.Escape) 28 | { 29 | mySource.Cancel(); 30 | } 31 | } 32 | 33 | private CancellationTokenSource mySource = new CancellationTokenSource(); 34 | 35 | private void Do() 36 | { 37 | mySource = new CancellationTokenSource(); 38 | 39 | const int ntasks = 4; 40 | long n; 41 | if (long.TryParse(N.Text, out n) && n >= 0) 42 | { 43 | var tasks = new Task[ntasks]; 44 | 45 | for (var i = 0; i < ntasks; i++) 46 | { 47 | tasks[i] = CreateTask(i*n/ntasks, (i + 1)*n/ntasks, mySource.Token); 48 | var currentContext = SynchronizationContext.Current; 49 | var currentScheduler = TaskScheduler.Current; 50 | 51 | tasks[i].Start(); 52 | //tasks[i].RunSynchronously(); 53 | //tasks[i].Start(TaskScheduler.FromCurrentSynchronizationContext()); 54 | } 55 | 56 | 57 | Task.Factory.ContinueWhenAll(tasks, _ => 58 | { 59 | try 60 | { 61 | //Task.WaitAll(tasks, mySource.Token); 62 | //WaitAndPump(tasks); 63 | 64 | var res = tasks.Aggregate(0, (acc, task) => acc + task.Result); 65 | Answer.Text = res.ToString(); 66 | } 67 | catch (AggregateException e) 68 | { 69 | Answer.Text = e.Flatten().InnerExceptions[0].Message; 70 | } 71 | }, CancellationToken.None, TaskContinuationOptions.None, TaskScheduler.FromCurrentSynchronizationContext()); 72 | 73 | } 74 | else 75 | { 76 | Answer.Text = "Invalid number: " + N.Text; 77 | } 78 | } 79 | 80 | private static Task CreateTask(long start, long end, CancellationToken token) 81 | { 82 | return new Task(() => 83 | { 84 | Console.WriteLine(Thread.CurrentThread.ManagedThreadId); 85 | 86 | var res = 0; 87 | for (var x = Math.Max(start + 1, 2); x <= end;) 88 | { 89 | for (var j = 2; j*j <= x; j++) 90 | if (x%j == 0) goto outer; 91 | 92 | res++; 93 | 94 | outer: 95 | x++; 96 | 97 | token.ThrowIfCancellationRequested(); 98 | } 99 | 100 | 101 | return res; 102 | }, token); 103 | } 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | public void WaitAndPump(Task[] tasks) 123 | { 124 | while (true) 125 | { 126 | if (tasks.All(t => t.IsCompleted)) break; 127 | 128 | DoEvents(); 129 | } 130 | } 131 | 132 | 133 | [SecurityPermission(SecurityAction.Demand, Flags = SecurityPermissionFlag.UnmanagedCode)] 134 | private void DoEvents() 135 | { 136 | DispatcherFrame frame = new DispatcherFrame(); 137 | Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background, 138 | new DispatcherOperationCallback(ExitFrame), frame); 139 | Dispatcher.PushFrame(frame); 140 | } 141 | 142 | private object ExitFrame(object f) 143 | { 144 | ((DispatcherFrame) f).Continue = false; 145 | 146 | return null; 147 | } 148 | } 149 | } -------------------------------------------------------------------------------- /ParallelDemo/ParallelDemo.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {5BC69670-B51C-4E2B-A167-E8C2D6C07EB0} 8 | WinExe 9 | Properties 10 | ParallelDemo 11 | ParallelDemo 12 | v4.5 13 | 512 14 | {60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} 15 | 4 16 | 17 | 18 | AnyCPU 19 | true 20 | full 21 | false 22 | bin\Debug\ 23 | DEBUG;TRACE 24 | prompt 25 | 4 26 | 27 | 28 | AnyCPU 29 | pdbonly 30 | true 31 | bin\Release\ 32 | TRACE 33 | prompt 34 | 4 35 | 36 | 37 | 38 | ..\packages\NUnit.2.6.4\lib\nunit.framework.dll 39 | True 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 4.0 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | MSBuild:Compile 58 | Designer 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | MSBuild:Compile 68 | Designer 69 | 70 | 71 | App.xaml 72 | Code 73 | 74 | 75 | MainWindow.xaml 76 | Code 77 | 78 | 79 | 80 | 81 | Code 82 | 83 | 84 | True 85 | True 86 | Resources.resx 87 | 88 | 89 | True 90 | Settings.settings 91 | True 92 | 93 | 94 | ResXFileCodeGenerator 95 | Resources.Designer.cs 96 | 97 | 98 | 99 | SettingsSingleFileGenerator 100 | Settings.Designer.cs 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 116 | -------------------------------------------------------------------------------- /ParallelDemo/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Resources; 3 | using System.Runtime.CompilerServices; 4 | using System.Runtime.InteropServices; 5 | using System.Windows; 6 | 7 | // General Information about an assembly is controlled through the following 8 | // set of attributes. Change these attribute values to modify the information 9 | // associated with an assembly. 10 | [assembly: AssemblyTitle("ParallelDemo")] 11 | [assembly: AssemblyDescription("")] 12 | [assembly: AssemblyConfiguration("")] 13 | [assembly: AssemblyCompany("")] 14 | [assembly: AssemblyProduct("ParallelDemo")] 15 | [assembly: AssemblyCopyright("Copyright © 2015")] 16 | [assembly: AssemblyTrademark("")] 17 | [assembly: AssemblyCulture("")] 18 | 19 | // Setting ComVisible to false makes the types in this assembly not visible 20 | // to COM components. If you need to access a type in this assembly from 21 | // COM, set the ComVisible attribute to true on that type. 22 | [assembly: ComVisible(false)] 23 | 24 | //In order to begin building localizable applications, set 25 | //CultureYouAreCodingWith in your .csproj file 26 | //inside a . For example, if you are using US english 27 | //in your source files, set the to en-US. Then uncomment 28 | //the NeutralResourceLanguage attribute below. Update the "en-US" in 29 | //the line below to match the UICulture setting in the project file. 30 | 31 | //[assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.Satellite)] 32 | 33 | 34 | [assembly: ThemeInfo( 35 | ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located 36 | //(used if a resource is not found in the page, 37 | // or application resource dictionaries) 38 | ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located 39 | //(used if a resource is not found in the page, 40 | // app, or any theme specific resource dictionaries) 41 | )] 42 | 43 | 44 | // Version information for an assembly consists of the following four values: 45 | // 46 | // Major Version 47 | // Minor Version 48 | // Build Number 49 | // Revision 50 | // 51 | // You can specify all the values or you can default the Build and Revision Numbers 52 | // by using the '*' as shown below: 53 | // [assembly: AssemblyVersion("1.0.*")] 54 | [assembly: AssemblyVersion("1.0.0.0")] 55 | [assembly: AssemblyFileVersion("1.0.0.0")] 56 | -------------------------------------------------------------------------------- /ParallelDemo/Properties/Resources.Designer.cs: -------------------------------------------------------------------------------- 1 | //------------------------------------------------------------------------------ 2 | // 3 | // This code was generated by a tool. 4 | // Runtime Version:4.0.30319.34014 5 | // 6 | // Changes to this file may cause incorrect behavior and will be lost if 7 | // the code is regenerated. 8 | // 9 | //------------------------------------------------------------------------------ 10 | 11 | namespace ParallelDemo.Properties 12 | { 13 | 14 | 15 | /// 16 | /// A strongly-typed resource class, for looking up localized strings, etc. 17 | /// 18 | // This class was auto-generated by the StronglyTypedResourceBuilder 19 | // class via a tool like ResGen or Visual Studio. 20 | // To add or remove a member, edit your .ResX file then rerun ResGen 21 | // with the /str option, or rebuild your VS project. 22 | [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] 23 | [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] 24 | [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] 25 | internal class Resources 26 | { 27 | 28 | private static global::System.Resources.ResourceManager resourceMan; 29 | 30 | private static global::System.Globalization.CultureInfo resourceCulture; 31 | 32 | [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] 33 | internal Resources() 34 | { 35 | } 36 | 37 | /// 38 | /// Returns the cached ResourceManager instance used by this class. 39 | /// 40 | [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] 41 | internal static global::System.Resources.ResourceManager ResourceManager 42 | { 43 | get 44 | { 45 | if ((resourceMan == null)) 46 | { 47 | global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("ParallelDemo.Properties.Resources", typeof(Resources).Assembly); 48 | resourceMan = temp; 49 | } 50 | return resourceMan; 51 | } 52 | } 53 | 54 | /// 55 | /// Overrides the current thread's CurrentUICulture property for all 56 | /// resource lookups using this strongly typed resource class. 57 | /// 58 | [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] 59 | internal static global::System.Globalization.CultureInfo Culture 60 | { 61 | get 62 | { 63 | return resourceCulture; 64 | } 65 | set 66 | { 67 | resourceCulture = value; 68 | } 69 | } 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /ParallelDemo/Properties/Resources.resx: -------------------------------------------------------------------------------- 1 |  2 | 3 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | text/microsoft-resx 107 | 108 | 109 | 2.0 110 | 111 | 112 | System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 113 | 114 | 115 | System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 116 | 117 | -------------------------------------------------------------------------------- /ParallelDemo/Properties/Settings.Designer.cs: -------------------------------------------------------------------------------- 1 | //------------------------------------------------------------------------------ 2 | // 3 | // This code was generated by a tool. 4 | // Runtime Version:4.0.30319.34014 5 | // 6 | // Changes to this file may cause incorrect behavior and will be lost if 7 | // the code is regenerated. 8 | // 9 | //------------------------------------------------------------------------------ 10 | 11 | namespace ParallelDemo.Properties 12 | { 13 | 14 | 15 | [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] 16 | [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "11.0.0.0")] 17 | internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase 18 | { 19 | 20 | private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); 21 | 22 | public static Settings Default 23 | { 24 | get 25 | { 26 | return defaultInstance; 27 | } 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /ParallelDemo/Properties/Settings.settings: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /ParallelDemo/ReadWriteLock/ReadWriteLockSlimEx.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading; 3 | 4 | namespace ParallelDemo.ReadWriteLock 5 | { 6 | public struct ReadLockSlimCookie : IDisposable 7 | { 8 | private readonly ReaderWriterLockSlim myRwlock; 9 | 10 | internal ReadLockSlimCookie(ReaderWriterLockSlim rwlock) 11 | { 12 | rwlock.EnterReadLock(); 13 | myRwlock = rwlock; 14 | } 15 | 16 | public void Dispose() 17 | { 18 | if (myRwlock != null) myRwlock.ExitReadLock(); 19 | } 20 | } 21 | 22 | 23 | public struct WriteLockSlimCookie : IDisposable 24 | { 25 | private readonly ReaderWriterLockSlim myRwlock; 26 | 27 | internal WriteLockSlimCookie(ReaderWriterLockSlim rwlock) 28 | { 29 | rwlock.EnterWriteLock(); 30 | myRwlock = rwlock; 31 | } 32 | 33 | public void Dispose() 34 | { 35 | if (myRwlock != null) myRwlock.ExitWriteLock(); 36 | } 37 | } 38 | 39 | 40 | 41 | 42 | public static class ReadWriteLockSlimEx 43 | { 44 | public static ReadLockSlimCookie UsingReadLock(this ReaderWriterLockSlim slim) 45 | { 46 | return new ReadLockSlimCookie(slim); 47 | } 48 | 49 | public static WriteLockSlimCookie UsingWriteLock(this ReaderWriterLockSlim slim) 50 | { 51 | return new WriteLockSlimCookie(slim); 52 | } 53 | } 54 | } -------------------------------------------------------------------------------- /ParallelDemo/ReadWriteLock/SimpleReadWriteLock.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Diagnostics; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading; 7 | using System.Threading.Tasks; 8 | 9 | namespace ParallelDemo.ReadWriteLock 10 | { 11 | class SimpleReadWriteLock 12 | { 13 | private readonly object myLock = new object(); 14 | 15 | private readonly ThreadLocal myReaders = new ThreadLocal(); 16 | private int myReaderCount; 17 | 18 | private int myWriterCount; 19 | private Thread myWriter; 20 | 21 | public void AcquireRead() 22 | { 23 | lock (myLock) 24 | { 25 | if (myWriter != Thread.CurrentThread) 26 | { 27 | while (myWriter != null) Monitor.Wait(myLock); 28 | } 29 | myReaders.Value++; 30 | myReaderCount++; 31 | } 32 | } 33 | 34 | public void ReleaseRead() 35 | { 36 | lock (myLock) 37 | { 38 | if (myReaders.Value <= 0) throw new InvalidOperationException("myReaders.Value <= 0"); 39 | Debug.Assert(myWriter == null || (myWriter == Thread.CurrentThread && myReaderCount == myReaders.Value)); 40 | 41 | --myReaders.Value; 42 | if (--myReaderCount == 0 && myWriter == null) Monitor.PulseAll(myLock); 43 | } 44 | } 45 | 46 | public void AcquireWrite() 47 | { 48 | lock (myLock) 49 | { 50 | if (myWriter == Thread.CurrentThread) 51 | { 52 | Debug.Assert(myWriterCount > 0); 53 | myWriterCount ++; 54 | } 55 | else 56 | { 57 | while (myWriterCount > 0 || myReaderCount > 0) Monitor.Wait(myLock); 58 | Debug.Assert(myWriter == null); 59 | myWriter = Thread.CurrentThread; 60 | myWriterCount++; 61 | } 62 | } 63 | } 64 | 65 | public void ReleaseWrite() 66 | { 67 | lock (myLock) 68 | { 69 | if (myWriter != Thread.CurrentThread) throw new InvalidOperationException("myWriter != Thread.CurrentThread"); 70 | Debug.Assert(myWriterCount > 0); 71 | if (--myWriterCount == 0) 72 | { 73 | myWriter = null; 74 | Monitor.PulseAll(myLock); 75 | } 76 | } 77 | } 78 | } 79 | 80 | 81 | struct SimpleReadLockCookie : IDisposable 82 | { 83 | private readonly SimpleReadWriteLock myRwLock; 84 | public SimpleReadLockCookie(SimpleReadWriteLock rwLock) { (myRwLock = rwLock).AcquireRead();} 85 | public void Dispose() { myRwLock.ReleaseRead(); } 86 | } 87 | 88 | struct SimpleWriteLockCookie : IDisposable 89 | { 90 | private readonly SimpleReadWriteLock myRwLock; 91 | public SimpleWriteLockCookie(SimpleReadWriteLock rwLock) { (myRwLock = rwLock).AcquireWrite();} 92 | public void Dispose() { myRwLock.ReleaseWrite(); } 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /ParallelDemo/ReadWriteLock/TestRWLock.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Diagnostics; 3 | using System.Threading; 4 | using System.Threading.Tasks; 5 | using NUnit.Framework; 6 | 7 | // ReSharper disable LocalizableElement 8 | 9 | namespace ParallelDemo.ReadWriteLock 10 | { 11 | 12 | 13 | public class TestRwLock 14 | { 15 | [Test] 16 | public void TestSimpleReadWriteLock() 17 | { 18 | var sentry = new SimpleReadWriteLock(); 19 | var sw = new Stopwatch(); 20 | sw.Start(); 21 | 22 | Task.WaitAll( 23 | //first read 24 | Task.Run(delegate 25 | { 26 | using (new SimpleReadLockCookie(sentry)) 27 | { 28 | Console.WriteLine("Acquired read: {0}ms, thread={1}", sw.ElapsedMilliseconds, 29 | Thread.CurrentThread.ManagedThreadId); 30 | 31 | Thread.Sleep(500); 32 | 33 | Console.WriteLine("Releasing read: {0}ms, thread={1}", sw.ElapsedMilliseconds, 34 | Thread.CurrentThread.ManagedThreadId); 35 | } 36 | }), 37 | 38 | //write 39 | Task.Run(async delegate 40 | { 41 | await Task.Delay(100); 42 | 43 | 44 | using (new SimpleWriteLockCookie(sentry)) 45 | { 46 | Console.WriteLine("Acquired write: {0}ms, thread={1}", sw.ElapsedMilliseconds, 47 | Thread.CurrentThread.ManagedThreadId); 48 | 49 | Thread.Sleep(500); 50 | Console.WriteLine("Releasing write: {0}ms, thread={1}", sw.ElapsedMilliseconds, 51 | Thread.CurrentThread.ManagedThreadId); 52 | } 53 | }), 54 | 55 | //read that starts after write but executes before 56 | Task.Run(async delegate 57 | { 58 | await Task.Delay(200); 59 | 60 | using (new SimpleReadLockCookie(sentry)) 61 | { 62 | using (new SimpleReadLockCookie(sentry)) 63 | { 64 | Console.WriteLine("Acquired read: {0}ms, thread={1}", sw.ElapsedMilliseconds, 65 | Thread.CurrentThread.ManagedThreadId); 66 | 67 | Thread.Sleep(500); 68 | 69 | Console.WriteLine("Releasing read: {0}ms, thread={1}", sw.ElapsedMilliseconds, 70 | Thread.CurrentThread.ManagedThreadId); 71 | } 72 | } 73 | }), 74 | 75 | //this read task must wait for write on 76 | Task.Run(async delegate 77 | { 78 | await Task.Delay(1000); 79 | 80 | using (new SimpleReadLockCookie(sentry)) 81 | { 82 | Console.WriteLine("Acquired read: {0}ms, thread={1}", sw.ElapsedMilliseconds, 83 | Thread.CurrentThread.ManagedThreadId); 84 | Thread.Sleep(500); 85 | 86 | Console.WriteLine("Releasing read: {0}ms, thread={1}", sw.ElapsedMilliseconds, 87 | Thread.CurrentThread.ManagedThreadId); 88 | } 89 | }) 90 | ); 91 | 92 | 93 | sw.Stop(); 94 | Console.WriteLine("Test finished"); 95 | } 96 | 97 | 98 | 99 | 100 | [Test] 101 | public void TestReadWriteLockSlim() 102 | { 103 | var sentry = new ReaderWriterLockSlim(); 104 | var sw = new Stopwatch(); 105 | sw.Start(); 106 | 107 | var barrier = new CountdownEvent(4); 108 | 109 | //first read 110 | ThreadPool.QueueUserWorkItem(_ => 111 | { 112 | using (sentry.UsingReadLock()) 113 | { 114 | Console.WriteLine("Acquired read: {0}ms, thread={1}", sw.ElapsedMilliseconds, 115 | Thread.CurrentThread.ManagedThreadId); 116 | 117 | Thread.Sleep(500); 118 | 119 | Console.WriteLine("Releasing read: {0}ms, thread={1}", sw.ElapsedMilliseconds, 120 | Thread.CurrentThread.ManagedThreadId); 121 | } 122 | barrier.Signal(); 123 | }); 124 | 125 | //write 126 | ThreadPool.QueueUserWorkItem(_ => 127 | { 128 | Thread.Sleep(100); 129 | 130 | using (sentry.UsingWriteLock()) 131 | { 132 | Console.WriteLine("Acquired write: {0}ms, thread={1}", sw.ElapsedMilliseconds, 133 | Thread.CurrentThread.ManagedThreadId); 134 | 135 | Thread.Sleep(500); 136 | Console.WriteLine("Releasing write: {0}ms, thread={1}", sw.ElapsedMilliseconds, 137 | Thread.CurrentThread.ManagedThreadId); 138 | } 139 | barrier.Signal(); 140 | }); 141 | 142 | //read that starts after write but executes before 143 | ThreadPool.QueueUserWorkItem(_ => 144 | { 145 | Thread.Sleep(200); 146 | 147 | using (sentry.UsingReadLock()) 148 | { 149 | 150 | Console.WriteLine("Acquired read: {0}ms, thread={1}", sw.ElapsedMilliseconds, 151 | Thread.CurrentThread.ManagedThreadId); 152 | 153 | Thread.Sleep(500); 154 | 155 | Console.WriteLine("Releasing read: {0}ms, thread={1}", sw.ElapsedMilliseconds, 156 | Thread.CurrentThread.ManagedThreadId); 157 | } 158 | barrier.Signal(); 159 | }); 160 | 161 | //this read task must wait for write on 162 | ThreadPool.QueueUserWorkItem(_ => 163 | { 164 | Thread.Sleep(1000); 165 | 166 | using (sentry.UsingReadLock()) 167 | { 168 | Console.WriteLine("Acquired read: {0}ms, thread={1}", sw.ElapsedMilliseconds, 169 | Thread.CurrentThread.ManagedThreadId); 170 | Thread.Sleep(500); 171 | 172 | Console.WriteLine("Releasing read: {0}ms, thread={1}", sw.ElapsedMilliseconds, 173 | Thread.CurrentThread.ManagedThreadId); 174 | } 175 | barrier.Signal(); 176 | }); 177 | 178 | barrier.Wait(); 179 | sw.Stop(); 180 | Console.WriteLine("Test finished"); 181 | } 182 | 183 | } 184 | } -------------------------------------------------------------------------------- /ParallelDemo/packages.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Demo project by Dima Ivanov at SPB .NET Meetup #3. For more information see http://www.slideshare.net/yu5k3/parallel-demo. 2 | --------------------------------------------------------------------------------