Solved

WaitForSingleObject()

Posted on 2002-07-08
7
2,696 Views
Last Modified: 2008-03-03
Hello Expert,

Have A Thread that in some point calls:
WaitForSingleObject(hEvent, INFINITE);

The event object is created like this:
hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);

At some other point,
SetEvent(hEvent);
is called.

The problem:
The thread does not response to the "signaling" of hEvent an remains blocked.

Can you think about a reason for this ?

TIA,
Oren.
0
Comment
Question by:orenm
7 Comments
 
LVL 12

Expert Comment

by:pjknibbs
ID: 7140293
Well, first obvious questions: are you sure the CreateEvent() call succeeded? What results do you get if you call GetLastError() after the CreateEvent() and SetEvent() calls?
0
 

Author Comment

by:orenm
ID: 7140403
The CreateEvent() and SetEvent() calls are successful:
GetLastError() returns with 0 and the message:
"The operation completed successfully."

Oren.
0
 
LVL 10

Expert Comment

by:makerp
ID: 7140475
try

CreateEvent(NULL,TRUE,FALSE,NULL)
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

Accepted Solution

by:
weicco earned 100 total points
ID: 7140979
How are threads created and how the event handle is passed to threads?

If you create new thread and doesn't enable handle inheritance or you created event without handle inheritance, new thread doesn't get handle to event.

But if you use global variable to store and access event handle this shouldn't be the problem.
0
 

Author Comment

by:orenm
ID: 7142349
Some more information:

Same process (no security inheritance needed).
The event handles are static/global and not passed to the threads.

The application creates 2 identical threads like this:
m_hThread = (HANDLE)_beginthreadex(0, 0, ..., ..., 0, 0);

This should be interesting:
The WaitForSingleObject() DOES continue in case that SetEvent() is called from the "twin" thread.
The original problem is true in case that SetEvent() is called by the "main" thread.

hope this new information will help to make conclusions.

Oren.

P.S.
Actually this threads code is embedded in some C++ classes but this doesn't look significant.
0
 
LVL 12

Expert Comment

by:pjknibbs
ID: 7142686
Er...just a second, if the main thread is blocking on a WaitForSingleObject() call, how is it ever going to reach the code which calls SetEvent()?
0
 

Author Comment

by:orenm
ID: 7142771
Well, the bug is found.

for the interested and the familiar with C++:
The event handler was created in a constructor which actually invoked more then once for some objects, so,
the WaitForSingleObject() and SetEvent() had the same name of event handler, but,
actually relating to different handlers.

Thanks any way.

weicco, you were the closest, so you get the points.
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
memory mapped I/O query 6 147
How do I install gcc 4.8.4 on a Linux Ubuntu 14.04 machine? 5 1,954
Line meaning 9 87
Global Keyboard Hooks Blocked 4 81
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question