Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Threading related question

Threading related question

Scheduled Pinned Locked Moved C / C++ / MFC
questionhelptutoriallearning
5 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    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.

    A S J M 4 Replies Last reply
    0
    • L Lost User

      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.

      A Offline
      A Offline
      Arjan Schouten
      wrote on last edited by
      #2

      See WaitForSingleObject in the MSDN and set the event when you want the Thread to stop. Arjan.

      1 Reply Last reply
      0
      • L Lost User

        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.

        S Offline
        S Offline
        Sparticus
        wrote on last edited by
        #3

        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-
        完成の円

        1 Reply Last reply
        0
        • L Lost User

          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.

          J Offline
          J Offline
          Jambolo
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • L Lost User

            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.

            M Offline
            M Offline
            Mike Nordell
            wrote on last edited by
            #5

            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).

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups