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. SuspendThread question

SuspendThread question

Scheduled Pinned Locked Moved C / C++ / MFC
questionhelp
4 Posts 4 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.
  • M Offline
    M Offline
    misha_grewal
    wrote on last edited by
    #1

    Hi all - Here is the problem. I need to create five worker threads in my win 32 dll and keep them alive throughout the host process life time. I need to call certain routines in these threads as per the host process's calls. I want to know what are the possible loopholes in this code. Is it correct or totally wrong? Your expert and experienced comments will be greatly appreciated.. The code looks like HANDLE m_hThreadF1 = 0; //global thread handles HANDLE m_hThreadF2 = 0; HANDLE m_hThreadF3 = 0; DLL::Function1() //this routine gets called by the host process { if (m_hThreadF1!=0) ResumeThread(m_hThreadF1) else m_hThreadF1 = CreateThread(,,ThreadFuncF1,0); //not in suspended mode //Wait on the above thread to finish, then return, probably via an event } ThreadFuncF1() //thread handler { //do the intended processing SuspendThread(m_hThreadF1) }

    T S W 3 Replies Last reply
    0
    • M misha_grewal

      Hi all - Here is the problem. I need to create five worker threads in my win 32 dll and keep them alive throughout the host process life time. I need to call certain routines in these threads as per the host process's calls. I want to know what are the possible loopholes in this code. Is it correct or totally wrong? Your expert and experienced comments will be greatly appreciated.. The code looks like HANDLE m_hThreadF1 = 0; //global thread handles HANDLE m_hThreadF2 = 0; HANDLE m_hThreadF3 = 0; DLL::Function1() //this routine gets called by the host process { if (m_hThreadF1!=0) ResumeThread(m_hThreadF1) else m_hThreadF1 = CreateThread(,,ThreadFuncF1,0); //not in suspended mode //Wait on the above thread to finish, then return, probably via an event } ThreadFuncF1() //thread handler { //do the intended processing SuspendThread(m_hThreadF1) }

      T Offline
      T Offline
      Toby Opferman
      wrote on last edited by
      #2

      Why do you call suspendthread? If you suspend the thread it will not exit and thus if you wait on the thread handle it will not ever be signaled. It's only signaled when the thread exits and unless you have another thread calling "ResumeTHread" that thread will just sit around. Also, I know you didn't do this up there but never suspend a thread from another thread; it could be holding a lock and thus deadlock the process since you don't know what it's doing when you suspend it. 8bc7c0ec02c0e404c0cc0680f7018827ebee

      1 Reply Last reply
      0
      • M misha_grewal

        Hi all - Here is the problem. I need to create five worker threads in my win 32 dll and keep them alive throughout the host process life time. I need to call certain routines in these threads as per the host process's calls. I want to know what are the possible loopholes in this code. Is it correct or totally wrong? Your expert and experienced comments will be greatly appreciated.. The code looks like HANDLE m_hThreadF1 = 0; //global thread handles HANDLE m_hThreadF2 = 0; HANDLE m_hThreadF3 = 0; DLL::Function1() //this routine gets called by the host process { if (m_hThreadF1!=0) ResumeThread(m_hThreadF1) else m_hThreadF1 = CreateThread(,,ThreadFuncF1,0); //not in suspended mode //Wait on the above thread to finish, then return, probably via an event } ThreadFuncF1() //thread handler { //do the intended processing SuspendThread(m_hThreadF1) }

        S Offline
        S Offline
        Stephen Hewitt
        wrote on last edited by
        #3

        See here for an interesting article about why you shouldn't (in general) call SuspendThread. If you want one thead to wait until another is finished try code like this:

        WaitForSingleObject(m_hThreadF1, INFINITE);
        

        A thread handle is a waitable object - It becomes signalled when the thread exist. Steve

        1 Reply Last reply
        0
        • M misha_grewal

          Hi all - Here is the problem. I need to create five worker threads in my win 32 dll and keep them alive throughout the host process life time. I need to call certain routines in these threads as per the host process's calls. I want to know what are the possible loopholes in this code. Is it correct or totally wrong? Your expert and experienced comments will be greatly appreciated.. The code looks like HANDLE m_hThreadF1 = 0; //global thread handles HANDLE m_hThreadF2 = 0; HANDLE m_hThreadF3 = 0; DLL::Function1() //this routine gets called by the host process { if (m_hThreadF1!=0) ResumeThread(m_hThreadF1) else m_hThreadF1 = CreateThread(,,ThreadFuncF1,0); //not in suspended mode //Wait on the above thread to finish, then return, probably via an event } ThreadFuncF1() //thread handler { //do the intended processing SuspendThread(m_hThreadF1) }

          W Offline
          W Offline
          whizano
          wrote on last edited by
          #4

          some tips, uh? the method, WaitforSingleObject(threadhandle, waittime) will return a value equal to WAIT_TIMEOUT if the thread is still alive (not in signalled state). instead of if(m_hThreadF1 != 0) try using this method with waittime as zero, as below: if(WaitForSingleObject(m_hThread, 0)==WAIT_TIMEOUT) // The thread s alive else // The thread s signalled, spawn new make sure that the pThread's m_autodelete s set to false. u mentioned, the thread s supposed to be alive till da end. in this case, it seems, ur thread func has to make a slight modification. ResumeThread(handle), will resume a thread from the point it has been suspended, so, in ur case, when the ThreadFuncF1 resumes, it jst exits!!! ThreadFuncF1() { do{ // do the intended processing // Finished Processing, Notify parent SuspendThread(m_hThreadF1) }while(TRUE); // do processing again } hope thes'll help. :sigh: thanks, -- 'whiz'

          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