Skip to content

Instantly share code, notes, and snippets.

@Konard
Last active December 17, 2015 01:09
Show Gist options
  • Save Konard/5526447 to your computer and use it in GitHub Desktop.
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.
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