2
Supose我有几个线程和委托运行的方法。多线程和委托调用
public delegate void Del(string threadId, string value, ref string result);
public event Del Analyze= null; // The external world can plug in a method
// this method runs simultanioulsy in several threads
private void threaded()
{
string s= null;
// yadayada s gets a value and I want a result back
// from the external world
string result = null;
is (Analyze != null)
Analyze("some thread id", s, ref result);
}
我知道,作为事件的方法必须按顺序同步线程安全的,等等,但会发生什么,如果
Analyze("some thread id", s, ref result);
被称为在同一时间?这可以吗?或者我需要同步分析,如:
lock(someobj)
{
Analyze("some thread id", s, ref result);
}
所以,问题更像是:是一个“事件”这样的线程的视图调用类的点(我知道我必须保证线程安全安全在方法的插入式)
是的,在使用多线程共享的资源时,您应该始终进行同步。 –
@ rory.ap这肯定取决于“Analyze(...)'是否修改状态? –
@ScottPerham我希望它能做到,否则在参数'ref'中没有意义。 – itsme86