Last active
December 17, 2015 01:09
-
-
Save Konard/5526447 to your computer and use it in GitHub Desktop.
ThreadHelpers is a class, that contains few methods to simplify development of multithread programs.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Threading; | |
namespace Konard.Helpers | |
{ | |
public static class ThreadHelpers | |
{ | |
public const int BarrierDefaultValue = int.MinValue; | |
public static void InitializeThreadBarrier(ref int barrier) | |
{ | |
barrier = ThreadHelpers.BarrierDefaultValue; | |
} | |
public static void OnlyFirstThreadCanContinue(ref int barrier) | |
{ | |
int result = Interlocked.CompareExchange(ref barrier, Thread.CurrentThread.ManagedThreadId, ThreadHelpers.BarrierDefaultValue); | |
if (barrier != Thread.CurrentThread.ManagedThreadId) | |
throw new Exception("Only first thread can continue execution."); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment