Problem with WaitForSingleObject
-
CWinThread* m_pcThread1; CWinThread* m_pcThread2; int a1[100], a2[100],a3[200]; para* a; int* a9; para* b; int* b9; int* ptr = a3; a = new para; b = new para; a->main_ptr = a1; a->val = 1; b->main_ptr = a2; b->val = 0; m_pcThread1 = AfxBeginThread(WorkerThreadProc,a,THREAD_PRIORITY_NORMAL,0,0,NULL); m_pcThread2 = AfxBeginThread(WorkerThreadProc,b,THREAD_PRIORITY_NORMAL,0,0,NULL); // MessageBox("Thread Started"); // if(WaitForSingleObject(m_pcThread1, INFINITE) == WAIT_OBJECT_0) WaitForSingleObject(m_pcThread1,INFINITE); WaitForSingleObject(m_pcThread2,INFINITE); a9 = a->main_ptr; b9 = b->main_ptr; for(int i = 0; i<100;i++) { *ptr = *a9; *(ptr+100) = *b9; ptr++; a9++; b9++; } MessageBox("Calculated"); ------------------------------------------------------------ Above code is not waiting for thread to complete. I can not understand what is the problem? please help me
-
CWinThread* m_pcThread1; CWinThread* m_pcThread2; int a1[100], a2[100],a3[200]; para* a; int* a9; para* b; int* b9; int* ptr = a3; a = new para; b = new para; a->main_ptr = a1; a->val = 1; b->main_ptr = a2; b->val = 0; m_pcThread1 = AfxBeginThread(WorkerThreadProc,a,THREAD_PRIORITY_NORMAL,0,0,NULL); m_pcThread2 = AfxBeginThread(WorkerThreadProc,b,THREAD_PRIORITY_NORMAL,0,0,NULL); // MessageBox("Thread Started"); // if(WaitForSingleObject(m_pcThread1, INFINITE) == WAIT_OBJECT_0) WaitForSingleObject(m_pcThread1,INFINITE); WaitForSingleObject(m_pcThread2,INFINITE); a9 = a->main_ptr; b9 = b->main_ptr; for(int i = 0; i<100;i++) { *ptr = *a9; *(ptr+100) = *b9; ptr++; a9++; b9++; } MessageBox("Calculated"); ------------------------------------------------------------ Above code is not waiting for thread to complete. I can not understand what is the problem? please help me
And what does your thread function look like?
It was ever thus, the Neophiles will always rush out and get 'The Latest Thing' at a high price and with all the inherent faults - Dalek Dave.
-
CWinThread* m_pcThread1; CWinThread* m_pcThread2; int a1[100], a2[100],a3[200]; para* a; int* a9; para* b; int* b9; int* ptr = a3; a = new para; b = new para; a->main_ptr = a1; a->val = 1; b->main_ptr = a2; b->val = 0; m_pcThread1 = AfxBeginThread(WorkerThreadProc,a,THREAD_PRIORITY_NORMAL,0,0,NULL); m_pcThread2 = AfxBeginThread(WorkerThreadProc,b,THREAD_PRIORITY_NORMAL,0,0,NULL); // MessageBox("Thread Started"); // if(WaitForSingleObject(m_pcThread1, INFINITE) == WAIT_OBJECT_0) WaitForSingleObject(m_pcThread1,INFINITE); WaitForSingleObject(m_pcThread2,INFINITE); a9 = a->main_ptr; b9 = b->main_ptr; for(int i = 0; i<100;i++) { *ptr = *a9; *(ptr+100) = *b9; ptr++; a9++; b9++; } MessageBox("Calculated"); ------------------------------------------------------------ Above code is not waiting for thread to complete. I can not understand what is the problem? please help me
You should capture the return value from
WaitForSingleObject
to see why it did not wait. You also need to check that your worker thread is actually doing some work rather than returning immediately. NOTE: Please enclose your code snippet in <pre></pre> tags (use the "code block" button) to make it more readable.Just say 'NO' to evaluated arguments for diadic functions! Ash
-
CWinThread* m_pcThread1; CWinThread* m_pcThread2; int a1[100], a2[100],a3[200]; para* a; int* a9; para* b; int* b9; int* ptr = a3; a = new para; b = new para; a->main_ptr = a1; a->val = 1; b->main_ptr = a2; b->val = 0; m_pcThread1 = AfxBeginThread(WorkerThreadProc,a,THREAD_PRIORITY_NORMAL,0,0,NULL); m_pcThread2 = AfxBeginThread(WorkerThreadProc,b,THREAD_PRIORITY_NORMAL,0,0,NULL); // MessageBox("Thread Started"); // if(WaitForSingleObject(m_pcThread1, INFINITE) == WAIT_OBJECT_0) WaitForSingleObject(m_pcThread1,INFINITE); WaitForSingleObject(m_pcThread2,INFINITE); a9 = a->main_ptr; b9 = b->main_ptr; for(int i = 0; i<100;i++) { *ptr = *a9; *(ptr+100) = *b9; ptr++; a9++; b9++; } MessageBox("Calculated"); ------------------------------------------------------------ Above code is not waiting for thread to complete. I can not understand what is the problem? please help me
Wait on the thread handle, not on the pointer to the thread class.
...byte till it megahertz... my donation to web rubbish
-
CWinThread* m_pcThread1; CWinThread* m_pcThread2; int a1[100], a2[100],a3[200]; para* a; int* a9; para* b; int* b9; int* ptr = a3; a = new para; b = new para; a->main_ptr = a1; a->val = 1; b->main_ptr = a2; b->val = 0; m_pcThread1 = AfxBeginThread(WorkerThreadProc,a,THREAD_PRIORITY_NORMAL,0,0,NULL); m_pcThread2 = AfxBeginThread(WorkerThreadProc,b,THREAD_PRIORITY_NORMAL,0,0,NULL); // MessageBox("Thread Started"); // if(WaitForSingleObject(m_pcThread1, INFINITE) == WAIT_OBJECT_0) WaitForSingleObject(m_pcThread1,INFINITE); WaitForSingleObject(m_pcThread2,INFINITE); a9 = a->main_ptr; b9 = b->main_ptr; for(int i = 0; i<100;i++) { *ptr = *a9; *(ptr+100) = *b9; ptr++; a9++; b9++; } MessageBox("Calculated"); ------------------------------------------------------------ Above code is not waiting for thread to complete. I can not understand what is the problem? please help me
As already suggested, you have to check the return value of every
API
function you call, otherwise... troubles. :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
Wait on the thread handle, not on the pointer to the thread class.
...byte till it megahertz... my donation to web rubbish
Good point. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
CWinThread* m_pcThread1; CWinThread* m_pcThread2; int a1[100], a2[100],a3[200]; para* a; int* a9; para* b; int* b9; int* ptr = a3; a = new para; b = new para; a->main_ptr = a1; a->val = 1; b->main_ptr = a2; b->val = 0; m_pcThread1 = AfxBeginThread(WorkerThreadProc,a,THREAD_PRIORITY_NORMAL,0,0,NULL); m_pcThread2 = AfxBeginThread(WorkerThreadProc,b,THREAD_PRIORITY_NORMAL,0,0,NULL); // MessageBox("Thread Started"); // if(WaitForSingleObject(m_pcThread1, INFINITE) == WAIT_OBJECT_0) WaitForSingleObject(m_pcThread1,INFINITE); WaitForSingleObject(m_pcThread2,INFINITE); a9 = a->main_ptr; b9 = b->main_ptr; for(int i = 0; i<100;i++) { *ptr = *a9; *(ptr+100) = *b9; ptr++; a9++; b9++; } MessageBox("Calculated"); ------------------------------------------------------------ Above code is not waiting for thread to complete. I can not understand what is the problem? please help me
OK, here is your problem: you are passing the returned value (CWinThread pointer) from AfxBeginThread to WaitForSingleObject, which is not correct. Instead, you should pass the HANDLE defined inside those m_pcThread1 and m_pcThread2. Like so: WaitForSingleObject(m_pcThread1->m_hThread,INFINITE); WaitForSingleObject(m_pcThread2->m_hThread,INFINITE);
-- Arman
-
CWinThread* m_pcThread1; CWinThread* m_pcThread2; int a1[100], a2[100],a3[200]; para* a; int* a9; para* b; int* b9; int* ptr = a3; a = new para; b = new para; a->main_ptr = a1; a->val = 1; b->main_ptr = a2; b->val = 0; m_pcThread1 = AfxBeginThread(WorkerThreadProc,a,THREAD_PRIORITY_NORMAL,0,0,NULL); m_pcThread2 = AfxBeginThread(WorkerThreadProc,b,THREAD_PRIORITY_NORMAL,0,0,NULL); // MessageBox("Thread Started"); // if(WaitForSingleObject(m_pcThread1, INFINITE) == WAIT_OBJECT_0) WaitForSingleObject(m_pcThread1,INFINITE); WaitForSingleObject(m_pcThread2,INFINITE); a9 = a->main_ptr; b9 = b->main_ptr; for(int i = 0; i<100;i++) { *ptr = *a9; *(ptr+100) = *b9; ptr++; a9++; b9++; } MessageBox("Calculated"); ------------------------------------------------------------ Above code is not waiting for thread to complete. I can not understand what is the problem? please help me
Hello, WaitForSingleObject takes a handle as first argument. You are passing a pointer to a CWinThread. Changing your code to
WaitForSingleObject( m_pcThread1->m_hThread, INFINITE );
WaitForSingleObject( m_pcThread2->m_hThread, INFINITE );should solve your issue. Regards Frank
-
Wait on the thread handle, not on the pointer to the thread class.
...byte till it megahertz... my donation to web rubbish
I wonder why that compiles for him...
> The problem with computers is that they do what you tell them to do and not what you want them to do. < > Leela: Fry, you're wasting your life sitting in front of that TV. You need to get out and see the real world. Fry: But this is HDTV. It's got better resolution than the real world < >Nothing is free in the universe.<
-
I wonder why that compiles for him...
> The problem with computers is that they do what you tell them to do and not what you want them to do. < > Leela: Fry, you're wasting your life sitting in front of that TV. You need to get out and see the real world. Fry: But this is HDTV. It's got better resolution than the real world < >Nothing is free in the universe.<
it compiles 'cause Microsoft made a horrible decision with kernel handles in NT - they made them all void pointers so any type of pointer is compatible with them. You can pass any old rubbish pointer into most kernel functions where it expects a kernel handle and the compiler can't tell the difference. Ironically this is unlike GDI handles which they'd already made specific structure pointers in Windows 3.1 - 3 years previously. Such is life... [1] If you're prone to messing this sort of thing up a lot (as I am) then you can make things a bit more type safe in C++ by defining an overload of WaitForSingleObject: DWORD WaitForSingleObject( CWinThread *thread_ptr, DWORD msec_to_wait ) { return WaitForSingleObject( *thread_ptr, msec_to_wait ); } which will catch this sort of error. It's a right pain in the bum to have to that though and you have to make sure that you include your API wrappers otherwise you're back to square one (and there's the old catch-22 - if you know enough to use the typesafe wrappers you know enough to not need to use them). Cheers, Ash [1] I suppose the difference was that in Windows 3.1 if you got a handle wrong you had to reboot as you'd trashed the system, in NT you'd just mess up a process.
-
it compiles 'cause Microsoft made a horrible decision with kernel handles in NT - they made them all void pointers so any type of pointer is compatible with them. You can pass any old rubbish pointer into most kernel functions where it expects a kernel handle and the compiler can't tell the difference. Ironically this is unlike GDI handles which they'd already made specific structure pointers in Windows 3.1 - 3 years previously. Such is life... [1] If you're prone to messing this sort of thing up a lot (as I am) then you can make things a bit more type safe in C++ by defining an overload of WaitForSingleObject: DWORD WaitForSingleObject( CWinThread *thread_ptr, DWORD msec_to_wait ) { return WaitForSingleObject( *thread_ptr, msec_to_wait ); } which will catch this sort of error. It's a right pain in the bum to have to that though and you have to make sure that you include your API wrappers otherwise you're back to square one (and there's the old catch-22 - if you know enough to use the typesafe wrappers you know enough to not need to use them). Cheers, Ash [1] I suppose the difference was that in Windows 3.1 if you got a handle wrong you had to reboot as you'd trashed the system, in NT you'd just mess up a process.
Damn, you are right, i should have thought of that...i must be getting old and senile...what are we talking about again? :)
> The problem with computers is that they do what you tell them to do and not what you want them to do. < > Leela: Fry, you're wasting your life sitting in front of that TV. You need to get out and see the real world. Fry: But this is HDTV. It's got better resolution than the real world < >Nothing is free in the universe.<