Threading related question
-
Hi, Consider a thread function:
MyThreadFunc()
{
// do something` Sleep(50000);`
// do something
return;
}
As can be seen above that my thread blocks in the middle. What i want is that whenever user close my application, all the threads no matter what are they doing should first end gracefully but immediately. But suppose that my thread is stick on some blocking operation as shown in above code. How can i immediately cancel this blocking call and ask the thread to exit properly?? Infact i am using events after every next statements to check their status and exit my threads, but under such situations where the thread is stuck in a long blocking call my application waits like a hell. I want to immediately finish this thread. How ??? Example: In MSN messenger there is an option of
cancel sign in
. If we select this option then the connecting thread "immediately" stops working. Of course this thread calls various blocking Winsock calls especially for domain resolving, but it ends immediately. -
Hi, Consider a thread function:
MyThreadFunc()
{
// do something` Sleep(50000);`
// do something
return;
}
As can be seen above that my thread blocks in the middle. What i want is that whenever user close my application, all the threads no matter what are they doing should first end gracefully but immediately. But suppose that my thread is stick on some blocking operation as shown in above code. How can i immediately cancel this blocking call and ask the thread to exit properly?? Infact i am using events after every next statements to check their status and exit my threads, but under such situations where the thread is stuck in a long blocking call my application waits like a hell. I want to immediately finish this thread. How ??? Example: In MSN messenger there is an option of
cancel sign in
. If we select this option then the connecting thread "immediately" stops working. Of course this thread calls various blocking Winsock calls especially for domain resolving, but it ends immediately.See WaitForSingleObject in the MSDN and set the event when you want the Thread to stop. Arjan.
-
Hi, Consider a thread function:
MyThreadFunc()
{
// do something` Sleep(50000);`
// do something
return;
}
As can be seen above that my thread blocks in the middle. What i want is that whenever user close my application, all the threads no matter what are they doing should first end gracefully but immediately. But suppose that my thread is stick on some blocking operation as shown in above code. How can i immediately cancel this blocking call and ask the thread to exit properly?? Infact i am using events after every next statements to check their status and exit my threads, but under such situations where the thread is stuck in a long blocking call my application waits like a hell. I want to immediately finish this thread. How ??? Example: In MSN messenger there is an option of
cancel sign in
. If we select this option then the connecting thread "immediately" stops working. Of course this thread calls various blocking Winsock calls especially for domain resolving, but it ends immediately.So what kind of blocking functions are you calling?
If you're using Winsock you can create a non-blocking socket and check to see if a connection has been made or a message has been sent/received using the select function. If you want, this select function can 1) not block at all (not recommended) or 2)block for a set amount of time.
If you are using sockets I would loop through: telling the select function to block for an undetectable fraction of a second, check to see if the operation has completed, and repeat until the operation either completes, is cancelled, or is timed out.
-Michael Anderson-
完成の円 -
Hi, Consider a thread function:
MyThreadFunc()
{
// do something` Sleep(50000);`
// do something
return;
}
As can be seen above that my thread blocks in the middle. What i want is that whenever user close my application, all the threads no matter what are they doing should first end gracefully but immediately. But suppose that my thread is stick on some blocking operation as shown in above code. How can i immediately cancel this blocking call and ask the thread to exit properly?? Infact i am using events after every next statements to check their status and exit my threads, but under such situations where the thread is stuck in a long blocking call my application waits like a hell. I want to immediately finish this thread. How ??? Example: In MSN messenger there is an option of
cancel sign in
. If we select this option then the connecting thread "immediately" stops working. Of course this thread calls various blocking Winsock calls especially for domain resolving, but it ends immediately.My suggestion is to make it appear that the thread exits immediately even though it doesn't. In your MSN message example, how do you know the thread stops immediately? If a dialog box goes away, maybe it wasn't controlled by the blocked thread. After all, how does it respond to your click on "cancel sign in" if the thread is blocked? Also, are you sure the MSN messenger thread is blocked? Maybe it made an asynchronous call and is processing messages until it gets a reply. If you absolutely have to have the thread exit immediately, you can call TerminateThread, but read the docs about why it's a bad idea.
-
Hi, Consider a thread function:
MyThreadFunc()
{
// do something` Sleep(50000);`
// do something
return;
}
As can be seen above that my thread blocks in the middle. What i want is that whenever user close my application, all the threads no matter what are they doing should first end gracefully but immediately. But suppose that my thread is stick on some blocking operation as shown in above code. How can i immediately cancel this blocking call and ask the thread to exit properly?? Infact i am using events after every next statements to check their status and exit my threads, but under such situations where the thread is stuck in a long blocking call my application waits like a hell. I want to immediately finish this thread. How ??? Example: In MSN messenger there is an option of
cancel sign in
. If we select this option then the connecting thread "immediately" stops working. Of course this thread calls various blocking Winsock calls especially for domain resolving, but it ends immediately.Shah Shehpori wrote: How can i immediately cancel this blocking call and ask the thread to exit properly? In addition to the obvious answers you'll get, I thought I'd add a little apparently-not-too-well-known-something. Depending on what the thread is blocked by, what you request might actually not be possible. There are instances when a thread hangs in the kernel, making it effectively impossible to terminate the process (the only way to terminate such a process is power off or hardware reset).