ReaderWriterLockSlimHelper
namespace Microshaoft.Web.ShareCache
{
using System;
using System.Linq;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using System.Diagnostics;
using Microshaoft;
public static class DataManager
{
//[STAThread]
static void Main(string[] args)
{
Console.ReadLine();
string r = string.Empty;
while ((r = Console.ReadLine()) != "q")
{
if (r == "w")
{
Tuple<string, DateTime, bool>[] a =
new Tuple<string, DateTime, bool>[]
{
new Tuple<string, DateTime, bool>("aspx cs AAA", DateTime.Now, true)
, new Tuple<string, DateTime, bool>("aspx cs BBB", DateTime.Now, false)
, new Tuple<string, DateTime, bool>("aspx cs CCC", DateTime.Now, true)
};
var list = a.ToList();
DataManager.ShareData = list;
ShareData
.ForEach
(
(x) =>
{
Console.WriteLine(x.Item2);
}
);
}
else
{
ParallelProcess();
}
}
Console.WriteLine(DataManager.F2);
Console.WriteLine(Environment.Version.ToString());
}
private static void ParallelProcess()
{
var stopWatch = Stopwatch.StartNew();
Parallel.For
(
0
, 10000 * 50
, new ParallelOptions()
{
MaxDegreeOfParallelism = 32
}
, (x) =>
{
var z =
DataManager.ShareData;
z.ForEach
(
(xx) =>
{
//Console.WriteLine(x.Item2);
}
);
}
);
stopWatch.Stop();
Console.WriteLine(stopWatch.ElapsedMilliseconds);
}
static DataManager()
{
Tuple<string, DateTime, bool>[] a = new Tuple<string, DateTime, bool>[]
{
new Tuple<string, DateTime, bool>("A", DateTime.Now, true)
, new Tuple<string, DateTime, bool>("B", DateTime.Now, false)
, new Tuple<string, DateTime, bool>("C", DateTime.Now, true)
};
ShareData = a.ToList();
}
private static List<Tuple<string, DateTime, bool>> _shareData;
private static ReaderWriterLockSlim _readerWriterLockSlim = new ReaderWriterLockSlim();
public static List<Tuple<string, DateTime, bool>> ShareData
{
set
{
_readerWriterLockSlim
.TryEnterWriterLockSlimWrite<List<Tuple<string, DateTime, bool>>>(ref _shareData, value, 2);
}
get
{
return
_readerWriterLockSlim
.TryEnterReadLockSlimRead<List<Tuple<string, DateTime, bool>>>
(
(x) =>
{
return _shareData;
}
, 10
);
//_shareData;
}
}
public volatile static int F1 = 99, F2 = 100;
}
}
namespace Microshaoft
{
using System;
using System.Threading;
public static class ReaderWriterLockSlimExtensionsMethodsManager
{
public static bool TryEnterWriterLockSlimWrite<T>
(
this ReaderWriterLockSlim writerLockSlim
, ref T target
, T newTarget
, int enterTimeOutInSeconds
)
where T : class
{
return
ReaderWriterLockSlimHelper
.TryEnterWriterLockSlimWrite<T>
(
writerLockSlim
, ref target
, newTarget
, enterTimeOutInSeconds
);
}
public static T TryEnterReadLockSlimRead<T>
(
this ReaderWriterLockSlim readerLockSlim
, Func<ReaderWriterLockSlim, T> onReadedProcessFunc
, int enterTimeOutInSeconds
)
{
return
ReaderWriterLockSlimHelper
.TryEnterReadLockSlimRead<T>
(
readerLockSlim
, onReadedProcessFunc
, enterTimeOutInSeconds
);
}
public static bool TryEnterLockSlim
(
this ReaderWriterLockSlim lockSlim
, Func<ReaderWriterLockSlim, bool> onEnterProcessFunc
, Action action
, Action<ReaderWriterLockSlim> onExitProcessAction
)
{
return
ReaderWriterLockSlimHelper
.TryEnterLockSlim
(
lockSlim
, onEnterProcessFunc
, action
, onExitProcessAction
);
}
}
public static class ReaderWriterLockSlimHelper
{
public static bool TryEnterWriterLockSlimWrite<T>
(
ReaderWriterLockSlim writerLockSlim
, ref T target
, T newTarget
, int enterTimeOutInSeconds
)
where T : class
{
bool r = false;
//var rwls = new ReaderWriterLockSlim();
int timeOut = Timeout.Infinite;
if (enterTimeOutInSeconds >= 0)
{
timeOut = enterTimeOutInSeconds * 1000;
}
try
{
r = (writerLockSlim.TryEnterWriteLock(timeOut));
if (r)
{
Interlocked.Exchange<T>(ref target, newTarget);
r = true;
}
}
finally
{
if (r)
{
writerLockSlim.ExitWriteLock();
}
}
return r;
}
public static T TryEnterReadLockSlimRead<T>
(
ReaderWriterLockSlim readerLockSlim
, Func<ReaderWriterLockSlim, T> onReadedProcessFunc
, int enterTimeOutInSeconds
)
{
T r = default(T);
var rr = false;
//var rwls = new ReaderWriterLockSlim();
int timeOut = Timeout.Infinite;
if (enterTimeOutInSeconds >= 0)
{
timeOut = enterTimeOutInSeconds * 1000;
}
try
{
rr = (readerLockSlim.TryEnterReadLock(timeOut));
if (rr)
{
r = onReadedProcessFunc(readerLockSlim);
rr = true;
}
}
finally
{
if (rr)
{
readerLockSlim.ExitReadLock();
}
}
return r;
}
public static bool TryEnterLockSlim
(
ReaderWriterLockSlim lockSlim
, Func<ReaderWriterLockSlim, bool> onEnterProcessFunc
, Action action
, Action<ReaderWriterLockSlim> onExitProcessAction
)
{
bool r = false;
if (action != null)
{
try
{
r = onEnterProcessFunc(lockSlim);
if (r)
{
action();
r = true;
}
}
finally
{
if (r)
{
onExitProcessAction(lockSlim);
}
}
}
return r;
}
}
}
|