So first let's create an enum of long and short straws.
enum Straw { Long, Short }
using System;
using System.Linq;
using System.Threading;
namespace pc.ThreadLock
{
enum Straw { Long, Short }
}
using System;
using System.Linq;
using System.Threading;
namespace pc.ThreadLock
{
enum Straw { Long, Short }
class Straws
{
public int Count { get { return _straws.Count(); } }
Straw[] _straws;
public Straw this[int Index]
{
get
{
var pulledStraw = _straws[Index];
//shift
straws from pulled one left and resize array
for (; Index + 1 < _straws.Length; Index++)
_straws[Index] =
_straws[Index + 1];
Array.Resize(ref _straws,
_straws.Length - 1);
return pulledStraw;
}
}
public Straws(int
NumOfStraws, int
NumOfShortStraws = 1)
{
_straws = new Straw[NumOfStraws];
while (NumOfShortStraws > 0)
{
//pick
random index for short straw, if selected index
//already
has short straw, try again.
var shortStrawIndex = Program.rnd.Next(NumOfStraws);
if (_straws[shortStrawIndex] == Straw.Short)
continue;
_straws[shortStrawIndex] = Straw.Short;
NumOfShortStraws--;
}
}
}
}
class Program
{
public static Random rnd = new Random(DateTime.Now.Millisecond);
static Straws Straws = new Straws(5,2);
static void Main(string[] args)
{
var Pawel = new Thread(new ThreadStart(PullStraw));
Pawel.Name = "Pawel";
var Magda = new Thread(new ThreadStart(PullStraw));
Magda.Name = "Magda";
var Jakub = new Thread(new ThreadStart(PullStraw));
Jakub.Name = "Jakub";
var Tomek = new Thread(new ThreadStart(PullStraw));
Tomek.Name = "Tomek";
var Marin = new Thread(new ThreadStart(PullStraw));
Marin.Name = "Marin";
var Threads = new Thread[] { Pawel, Magda, Jakub, Tomek,
Marin };
foreach (var t in Threads)
t.Start();
}
static void
PullStraw()
{
Straw PulledStraw = Straws[rnd.Next(Straws.Count - 1)];
var output = $"{Thread.CurrentThread.Name} pulled {PulledStraw.ToString()}";
Console.WriteLine(output);
}
}
to do this is actually pretty straight forward we are going to need an object to use as a lock outside of our threads scope, so at the program class level is fine.
using System;
using System.Linq;
using System.Threading;
namespace pc.ThreadLock
{
enum Straw { Long, Short }
class Straws
{
public int Count { get { return _straws.Count(); } }
Straw[] _straws;
public Straw this[int Index]
{
get
{
var pulledStraw = _straws[Index];
//shift
straws from pulled one left and resize array
for (; Index + 1 < _straws.Length; Index++)
_straws[Index] =
_straws[Index + 1];
Array.Resize(ref _straws,
_straws.Length - 1);
return pulledStraw;
}
}
public Straws(int
NumOfStraws, int
NumOfShortStraws = 1)
{
_straws = new Straw[NumOfStraws];
while (NumOfShortStraws > 0)
{
//pick
random index for short straw, if selected index
//already
has short straw, try again.
var shortStrawIndex = Program.rnd.Next(NumOfStraws);
if (_straws[shortStrawIndex] == Straw.Short)
continue;
_straws[shortStrawIndex] = Straw.Short;
NumOfShortStraws--;
}
}
}
class Program
{
public static Random rnd = new Random(DateTime.Now.Millisecond);
static Straws Straws = new Straws(5, 2);
//Our lock to
prevent concurrent access to the straws object
static object _lock = new object();
static void Main(string[] args)
{
var Pawel = new Thread(new ThreadStart(PullStraw));
Pawel.Name = "Pawel";
var Magda = new Thread(new ThreadStart(PullStraw));
Magda.Name = "Magda";
var Jakub = new Thread(new ThreadStart(PullStraw));
Jakub.Name = "Jakub";
var Tomek = new Thread(new ThreadStart(PullStraw));
Tomek.Name = "Tomek";
var Marin = new Thread(new ThreadStart(PullStraw));
Marin.Name = "Marin";
var Threads = new Thread[] { Pawel, Magda, Jakub, Tomek,
Marin };
foreach (var t in Threads)
t.Start();
}
static void
PullStraw()
{
Straw PulledStraw;
//restrict
access to just one thread at a time
lock (_lock)
{
PulledStraw =
Straws[rnd.Next(Straws.Count - 1)];
}
var output = $"{Thread.CurrentThread.Name} pulled {PulledStraw.ToString()}";
Console.WriteLine(output);
}
}
}