469,578 Members | 1,742 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,578 developers. It's quick & easy.

BUG: Unified Event Model (Native) inject empty Lock/Unlock()

M.C
Following is a snap-shot of compiler created code when using UEM, use /Fx:

struct __EventCriticalSectionStub
{
void Lock() {}
void Unlock() {}
};

....

__EventCriticalSectionStub __EventingCS;

__EventingCS.Lock() and __EventingCS.Unlock() is called in several important
section such as __AddEventHandler__XXXX and firing event. But due to the flaw
in __EventCriticalSectionStub, corruption happen when using UEM in
multithread project.
From MSDN: Visual C++ Concepts: Adding Functionality - > Introduction to the
Unified Event Model

"..The model supports single- and multithreaded usage and protects data from
simultaneous multithread access..."
Am I missing something, or Visual C ...oops, I did it again?
Nov 17 '05 #1
1 1241
M.C
#define THREAD_COUNT 64
// Comment the following to see the problem
#define EXPLICIT_LOCK
[event_source(native, optimize=speed)]
class CSource
{
public:
__event void Print(LPCTSTR psz);

};
[event_receiver(native)]
class CReceiver
{
private:
#ifdef EXPLICIT_LOCK
CComAutoCriticalSection g_Lock;
#endif

public:
void Hook(CSource* p)
{
__hook(&CSource::Print, p, &CReceiver::Print);
}

void Print(LPCTSTR psz)
{
static TCHAR sz[64] = {0};
#ifdef EXPLICIT_LOCK
g_Lock.Lock();
#endif
::lstrcpy(sz, psz);
for(int i=0; i < ::lstrlen(psz); i++)
_puttch(sz[i]);
::ZeroMemory(sz, sizeof(sz));
#ifdef EXPLICIT_LOCK
g_Lock.Unlock();
#endif
}
};
DWORD WINAPI ThreadProc(LPVOID pParam)
{
CSource* ps = static_cast<CSource*>(pParam);
TCHAR sz[64] = {0};

::wsprintf(sz, _T("Hello World! (%d)\n"), ::GetCurrentThreadId());
::Sleep(rand() % 1000);
__raise ps->Print(sz);
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
HANDLE h[THREAD_COUNT] = {NULL};
CSource s;
CReceiver r;
r.Hook(&s);

srand((unsigned)time(NULL));
for(int i=0; i < THREAD_COUNT; i++)
h[i] = DefaultThreadTraits::CreateThread(NULL, 0, ThreadProc, &s, 0, NULL);
::WaitForMultipleObjects(THREAD_COUNT, h, TRUE, INFINITE);
for(i=0; i < THREAD_COUNT; i++)
::CloseHandle(h[i]);
return 0;
}
Nov 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Kevin Jiang | last post: by
8 posts views Thread by Jun | last post: by
16 posts views Thread by Edward Diener | last post: by
1 post views Thread by Bill | last post: by
reply views Thread by Kurt B. Kaiser | last post: by
45 posts views Thread by Chris Forone | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.