EventWaitHandle tem MemoryBarrier implícito?
Novo neste site, informe-me se não estiver postando de uma maneira aceita.
Eu frequentemente codifiquei algo ao longo das linhas da amostra abaixo (com coisas como Dispose omitidas para maior clareza.). Minha pergunta é: os voláteis são necessários, como mostrado? Ou o ManualResetEvent.Set tem uma barreira de memória implícita como eu li Thread.Start? Ou uma chamada explícita ao MemoryBarrier seria melhor que os voláteis? Ou é completamente errado? Além disso, o fato de o "comportamento implícito da barreira da memória" em algumas operações não estar documentado, até onde eu vi, é bastante frutífero, existe uma lista dessas operações em algum lugar?
Obrigado, Tom
:
class OneUseBackgroundOp
{
// background args
private string _x;
private object _y;
private long _z;
// background results
private volatile DateTime _a
private volatile double _b;
private volatile object _c;
// thread control
private Thread _task;
private ManualResetEvent _completedSignal;
private volatile bool _completed;
public bool DoSomething(string x, object y, long z, int initialWaitMs)
{
bool doneWithinWait;
_x = x;
_y = y;
_z = z;
_completedSignal = new ManualResetEvent(false);
_task = new Thread(new ThreadStart(Task));
_task.IsBackground = true;
_task.Start()
doneWithinWait = _completedSignal.WaitOne(initialWaitMs);
return doneWithinWait;
}
public bool Completed
{
get
{
return _completed;
}
}
/* public getters for the result fields go here, with an exception
thrown if _completed is not true; */
private void Task()
{
// args x, y, and z are written once, before the Thread.Start
// implicit memory barrier so they may be accessed freely.
// possibly long-running work goes here
// with the work completed, assign the result fields _a, _b, _c here
_completed = true;
_completedSignal.Set();
}
}