Windows threads waitforsingleobject




















Then it creates several reader threads. The master thread performs a write operation and then sets the event object to the signaled state when it has finished writing. Before starting a read operation, each reader thread uses WaitForSingleObject to wait for the manual-reset event object to be signaled. When WaitForSingleObject returns, this indicates that the main thread is ready for it to begin its read operation.

The return value from the function call is a handler for the thread, which is a different construct than the thread ID. When the call was unsuccessful, it returns 0. With the exception of the address of the function to execute, all of the parameters will take default values if they are provided the null.

The following code shows how to create a child thread using the CreateThread. It also captures the ID of the created thread. The threadID is not very useful since most functions take the thread handle as a parameter.

CreateThread tells the OS to make a new thread. But it does not set up the thread to work with the libraries provided by the developer environment. In other words, though Windows creates the thread and returns a handle to that thread, the runtime libraries haven't set up the thread-local data structures that they need.

So, instead of calling CreateThread , we should use the calls by the runtime libraries. This will vary from compiler to compiler. There is another difference between these two functions other than the parameters they take. The call to WaitForSingleObject waits for an object to signal its readiness. In other words, the routine passes the handle to a thread and waits for that thread to terminate.

Threads created in main will not continue to run after the main thread exits. So, the main thread must wait for the threads it created to complete before it exits the main function. It does this by calling function WaitForMultipleObjects. Therefore, it is impossible to query the status of the thread or even be confident that the handle to the thread is a handle to the same thread to which is originally pointed. The following example demonstrates this issue:.

The mythreadA terminates quickly and may have already terminated by the time that the main thread reaches the call to create the second thread. If the first thread has terminated, the handle to the first thread may be reused as the handle to the second thread. Queries using the handle of the first thread might succeed, but they will work on the wrong thread.

The calls to WaitForSingleObject may not be using a correct or valid handle for either of the threads depending on the completion time of the threads. Synchronization Functions. Wait Functions. Skip to main content. This browser is no longer supported. Download Microsoft Edge More info.

Contents Exit focus mode. WaitForSingleObject function synchapi. Please rate your experience Yes No. Any additional feedback? The WaitForSingleObject always return a timeout even if the created thread end normally the event wortks fine and it exits. From what I understand, the problem is that I call SetEvent in the main thread and then immediately after that I call WaitForSingleObject also in the main thread to wait for the other thread to end.

It seems that the main thread is "stopped" because of that WaitForSingleObject so it cannot see the ending of the other thread. You need to do a careful code review to search for buffer overruns, double deletes, etc. IMHO this is the most reliable way of detecting heap problems. Your description of the implementation sound just fine. But I don't understand what you are observing.

Are you saying that in spite of the thread terminating, the main thread continues to wait at WaitForSingleObject until it times out? Do you have any idea of where I should look at to find this cause? I tried to eliminate a lot of stuff from my code and I still experience this problem.



0コメント

  • 1000 / 1000