我有一个简单的类,它以异步方式发送请求。
public class MyClass
{
private readonly ISender _sender;
public MyClass(ISender sender)
{
_sender = sender;
}
public Task<string> SendAsync(string input, CancellationToken cancellationToken)
{
return _sender.SendAsync(input, cancellationToken);
}
}
public interface ISender
{
Task<string> SendAsync(string input, CancellationToken cancellationToken);
}
所有看起来都很简单,直到满足以下要求:_sender可以在运行时更改。
MyClass的新实现:
public class MyClass
{
private readonly ISender _sender;
public MyClass(ISender sender)
{
_sender = sender;
}
public Task<string> SendAsync(string input, CancellationToken cancellationToken)
{
return _sender.SendAsync(input, cancellationToken);
}
public void SenderChanged(object unused, SenderEventArgs e)
{
ISender previous = Interlocked.Exchange(ref _sender, SenderFactory.Create(e.NewSenderConfig));
previous.Dispose();
}
}
显然这段代码不是线程安全的。我需要在lock
和SendAsync
中引入SenderChanged
以确保_sender始终是最新的对象。
但是我希望每天调用一次SenderChanged
,并且SendAsync
(读取_sender对象)被称为10000 /秒。
Lock
和上下文切换会破坏此代码的性能。
是否有可能通过低级别锁定来处理此问题?或者你知道上述要求后如何解决这个问题呢?
答案 0 :(得分:3)
通常的方法是使用读写器锁,特别是ReaderWriterLockSlim。这是一个类似监视器的锁,可以优化频繁的读访问和不频繁的写访问,并且它支持多个并发读取器和单个编写器,这似乎正是您的用例。
然而,它似乎确实以适中的成本出现。我编写了两个测试 - 一个使用ReaderWriterLockSlim
来正确执行操作,另一个使用您的实现,唯一的更改是处置异常重试循环。在我的情况下,我改变了发送者20次,每10秒一次。这比您建议的用例要短得多,但确实可以作为性能差异的估计值。
最后:
工作单位&#39;正在调用调用Thread.SpinWait(100)
的DoWork方法。如果您想自己测试,请在下面发布代码。
修改:
我调整了Thread.SpinWait()
电话,以更改锁定与“工作”所花费的时间之间的平衡。在我的机器上旋转等待大约900-1000,两个实现都运行相同,大约1000个工作单位/毫秒。从上面的结果来看,这应该是显而易见的,但我确实想要进行健全检查。
原来,原始结果显示我们每秒使用锁处理大约280万个请求;至少在我的机器上是一个4核Intel CPU,&#34; Intel Core 2 Quad CPU Q9650 @ 3.00 GHz&#34;。鉴于您正在努力争取10k请求/秒,看起来您在锁定开始成为CPU使用量的很大一部分之前已经达到了一个数量级的余量。
#define USE_READERWRITER
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace TestProject
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
SenderDispatch dispatch = new SenderDispatch();
List<Worker> workers = new List<Worker>();
workers.Add( new Worker( dispatch, "A" ) );
workers.Add( new Worker( dispatch, "B" ) );
workers.Add( new Worker( dispatch, "C" ) );
workers.Add( new Worker( dispatch, "D" ) );
Thread.CurrentThread.Name = "Main thread";
Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.High;
Stopwatch watch = new Stopwatch();
watch.Start();
workers.ForEach( x => x.Start() );
for( int i = 0; i < 20; i++ )
{
Thread.Sleep( 10000 );
dispatch.NewSender();
}
Console.WriteLine( "Stopping..." );
workers.ForEach( x => x.Stop() );
watch.Stop();
Console.WriteLine( "Stopped" );
long sum = workers.Sum( x => x.FinalCount );
string message =
"Sum of worker iterations: " + sum.ToString( "n0" ) + "\r\n" +
"Total time: " + ( watch.ElapsedMilliseconds / 1000.0 ).ToString( "0.000" ) + "\r\n" +
"Iterations/ms: " + sum / watch.ElapsedMilliseconds;
MessageBox.Show( message );
}
}
public class Worker
{
private SenderDispatch dispatcher;
private Thread thread;
private bool working;
private string workerName;
public Worker( SenderDispatch dispatcher, string workerName )
{
this.dispatcher = dispatcher;
this.workerName = workerName;
this.working = false;
}
public long FinalCount { get; private set; }
public void Start()
{
this.thread = new Thread( Run );
this.thread.Name = "Worker " + this.workerName;
this.working = true;
this.thread.Start();
}
private void Run()
{
long state = 0;
while( this.working )
{
this.dispatcher.DoOperation( workerName, state );
state++;
}
this.FinalCount = state;
}
public void Stop()
{
this.working = false;
this.thread.Join();
}
}
public class SenderDispatch
{
private Sender sender;
private ReaderWriterLockSlim senderLock;
public SenderDispatch()
{
this.sender = new Sender();
this.senderLock = new ReaderWriterLockSlim( LockRecursionPolicy.NoRecursion );
}
public void DoOperation( string workerName, long value )
{
#if USE_READERWRITER
this.senderLock.EnterReadLock();
try
{
this.sender.DoOperation( workerName, value );
}
finally
{
this.senderLock.ExitReadLock();
}
#else
bool done = false;
do
{
try
{
this.sender.DoOperation( workerName, value );
done = true;
}
catch (ObjectDisposedException) { }
}
while( !done );
#endif
}
public void NewSender()
{
Sender prevSender;
Sender newSender;
newSender = new Sender();
#if USE_READERWRITER
this.senderLock.EnterWriteLock();
try
{
prevSender = Interlocked.Exchange( ref this.sender, newSender );
}
finally
{
this.senderLock.ExitWriteLock();
}
#else
prevSender = Interlocked.Exchange( ref this.sender, newSender );
prevSender.Dispose();
#endif
prevSender.Dispose();
}
}
public class Sender : IDisposable
{
private bool disposed;
public Sender()
{
this.disposed = false;
}
public void DoOperation( string workerName, long value )
{
if( this.disposed )
{
throw new ObjectDisposedException(
"Sender",
string.Format( "Worker {0} tried to queue work item {1}", workerName, value )
);
}
Thread.SpinWait( 100 );
}
public void Dispose()
{
this.disposed = true;
}
}
}