Click here to Skip to main content
15,938,218 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a c++ code where I create a thread when I push a button. When I push twice I have multithreading and I experiment how a thread waits for another by WaitForSingleObject() which apply on an Event object.
If I define the Event object as static, CreateEvent() in the constructor method, and OpenEvent at the begining of the function that implements the thread, all seems to be OK.
But if I define the Event object elsewhere, nonstatic, seems that the OpenEvent at the begining of the function that implements the thread doesn’t work properly and the thread doesn’t wait one for another,although the OpenEvent returns non null.

HANDLE hevent0;
hevent0=CreateEvent(NULL,TRUE,TRUE,_T("hevenim"));


HANDLE hevent;
hevent=OpenEvent(EVENT_ALL_ACCESS,TRUE, _T("hevenim"));
rwait=WaitForSingleObject(hevent,9999);

My question is why the OpenEvent doesn’t work properly in the second case?
Posted
Comments
[no name] 1-Oct-13 9:26am    
Why do you have 2 questions about basically the same issue? Limit yourself to one question and follow it through till you are clear about how threading works.

1 solution

Every thread has its own values. So you need a global or static event for syncronizing, or every thread is doing its own business.
 
Share this answer
 
Comments
argemi 1-Oct-13 7:58am    
It’s better to give a code example.

In this example does the OpenEvent function work properly or not ?

LONG CTestMsgDlg::m_nWorking = 0;
//HANDLE CTestMsgDlg::m_hevent;

CTestMsgDlg::CTestMsgDlg(CWnd* pParent /*=NULL*/)
: CDialog(CTestMsgDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CTestMsgDlg)
//}}AFX_DATA_INIT
HANDLE hevent;
hevent=CreateEvent(NULL,TRUE,TRUE,_T("hevenim")); //manual reset, signaled initial
//m_hevent=CreateEvent(NULL,TRUE,TRUE,_T("hevenim")); //manual reset, signaled initial
}

void CTestMsgDlg::_StartThread()
{
//::InterlockedExchange(&m_nWorking, 1);
CWinThread* pThread;
m_nWorking+=1;
pThread = AfxBeginThread(&CTestMsgDlg::WorkerThread, m_hWnd, THREAD_PRIORITY_NORMAL);
}

UINT AFX_CDECL CTestMsgDlg::WorkerThread(LPVOID lpParam)
//static function
{
int v2 = m_nWorking;
int v4;
long v5;
DWORD rwait;
BOOL eventset;
HANDLE hevent;
hevent=OpenEvent(EVENT_ALL_ACCESS,TRUE,_T("hevenim"));
//^^^ DOESN'T IT WORK PROPERLY ? ^^^^
while(m_nWorking)
{
CString* pString = new CString;
DWORD dwThreadID = ::GetCurrentThreadId();
SYSTEMTIME st = {0};
::GetLocalTime(&st);
int v1 = rand() % 999;
rwait=WaitForSingleObject(m_hevent,9999);
eventset=ResetEvent(m_hevent);
if (!eventset) return 0;
v9=v2;
for (v4=1; v4<222222222; v4++) v5=v4*v4/(v4+1) ;
pString->Format(_T("Posted by thread %X at %02u:%02u:%02u:%03u %02d >%X"),
dwThreadID, st.wHour, st.wMinute, st.wSecond, st.wMilliseconds, v9, v2);
eventset=SetEvent(m_hevent);
if (!eventset) return 0;
::PostMessage((HWND)lpParam, WM_APP_TIMESTAMP, 0, (LPARAM)pString);
}
return 0;
}
[no name] 1-Oct-13 8:57am    
Why is the OpenEvent call necessary? Make hevent global and create it non-signalled then set it after the thread is created.
argemi 1-Oct-13 15:42pm    
In this example OpenEvent call is not necessary, a global hevent is better. But I want to know if calling OpenEvent is ok, that is if the event object created with CreateEvent and named "hevenim" is an object that can be used in any function or thread of the current process, even though it isn't defined in a global scope. I can say that I don't get any error using OpenEvent like in above example.
KarstenK 1-Oct-13 8:11am    
I strongly recommand to use "Global/hevenim" to ensure that only one event per machine could be created.

I remember somehow that an error "already exists" can happen...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900