Reputation: 1511
I debug a complex application which crashes and I suspect that a memory between threads is not synchronized correctly. Here is a simplified example which demonstrates a potential problem:
DWORD WINAPI ThreadFun(LPVOID lpParam)
{
T* t = (T*)lpParam;
WaitForSingleObject(t->startProcessing, INFINITE);
//fill buffer with some data
for (int i = 0; i < bufSize; ++i)
{
t->buf[i] = i;
}
return 0;
}
//Main thread
void T::Process(int* p)
{
buf = p;
SetEvent(startProcessing);
}
There is a logic time relation - buf is set before calling SetEvent but does it guarentee that a memory will be synchronized between threads ? Should I use win api mutexes ?
Upvotes: 2
Views: 214
Reputation: 68691
SetEvent
and WaitForSingleObject
do provide the guarantee that writes before the SetEvent
are visible after WaitForSingleObject
returns. There is no need to provide anything else here.
Upvotes: 4