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. Help on multi-threading

Help on multi-threading

Scheduled Pinned Locked Moved C / C++ / MFC
debugginghelpquestion
9 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.
  • A Offline
    A Offline
    Abin
    wrote on last edited by
    #1

    I'm studying multi-threading today and got some major problems, the biggest one is that the worker thread is not terminating as expected, my code looks like this:// global BOOL g_bAbort = FALSE; UINT ThreadProc(LPVOID lpParam) { while (!g_bAbort) { // DoSomething(); // do some thing } return 0; } // in some class named "CThreadTestDlg" void CThreadTestDlg::OnStart() { // begin the thread g_bAbort = FALSE; m_pThread = ::AfxBeginThread(ThreadProc, NULL); } void CThreadTestDlg::OnStop() { // wanna stop the thread... g_bAbort = TRUE; DWORD dwExitCode = 0; do { Sleep(200); ::GetExitCodeThread(m_pThread->m_hThread, &dwExitCode); } while (dwExitCode == STILL_ACTIVE); }
    OnStart() is called as soon as the program starts, OnStop() is called when a button captioned ""stop" is pressed. I press the "stop" button and my program freezes. I used debugger and found out that the thread never ended, even though g_bAbort has been set to TRUE. Why so? Thank you a lot.

    S P R T 4 Replies Last reply
    0
    • A Abin

      I'm studying multi-threading today and got some major problems, the biggest one is that the worker thread is not terminating as expected, my code looks like this:// global BOOL g_bAbort = FALSE; UINT ThreadProc(LPVOID lpParam) { while (!g_bAbort) { // DoSomething(); // do some thing } return 0; } // in some class named "CThreadTestDlg" void CThreadTestDlg::OnStart() { // begin the thread g_bAbort = FALSE; m_pThread = ::AfxBeginThread(ThreadProc, NULL); } void CThreadTestDlg::OnStop() { // wanna stop the thread... g_bAbort = TRUE; DWORD dwExitCode = 0; do { Sleep(200); ::GetExitCodeThread(m_pThread->m_hThread, &dwExitCode); } while (dwExitCode == STILL_ACTIVE); }
      OnStart() is called as soon as the program starts, OnStop() is called when a button captioned ""stop" is pressed. I press the "stop" button and my program freezes. I used debugger and found out that the thread never ended, even though g_bAbort has been set to TRUE. Why so? Thank you a lot.

      S Offline
      S Offline
      Scott H Settlemier
      wrote on last edited by
      #2

      CWinThread normally has m_bAutoDelete set TRUE which means it will delete itself and close the m_hThread handle upon termination. You should either reset the m_bAutoDelete flag and delete it yourself or else use DuplicateHandle to create your own copy of the m_hThread handle which will be valid in your OnStop function.

      A 1 Reply Last reply
      0
      • S Scott H Settlemier

        CWinThread normally has m_bAutoDelete set TRUE which means it will delete itself and close the m_hThread handle upon termination. You should either reset the m_bAutoDelete flag and delete it yourself or else use DuplicateHandle to create your own copy of the m_hThread handle which will be valid in your OnStop function.

        A Offline
        A Offline
        Abin
        wrote on last edited by
        #3

        But the thread did not terminate, the "exit code" is always STILL_ACTIVE.

        S 1 Reply Last reply
        0
        • A Abin

          But the thread did not terminate, the "exit code" is always STILL_ACTIVE.

          S Offline
          S Offline
          Scott H Settlemier
          wrote on last edited by
          #4

          What's the return value of GetExitCodeThread?

          A 1 Reply Last reply
          0
          • A Abin

            I'm studying multi-threading today and got some major problems, the biggest one is that the worker thread is not terminating as expected, my code looks like this:// global BOOL g_bAbort = FALSE; UINT ThreadProc(LPVOID lpParam) { while (!g_bAbort) { // DoSomething(); // do some thing } return 0; } // in some class named "CThreadTestDlg" void CThreadTestDlg::OnStart() { // begin the thread g_bAbort = FALSE; m_pThread = ::AfxBeginThread(ThreadProc, NULL); } void CThreadTestDlg::OnStop() { // wanna stop the thread... g_bAbort = TRUE; DWORD dwExitCode = 0; do { Sleep(200); ::GetExitCodeThread(m_pThread->m_hThread, &dwExitCode); } while (dwExitCode == STILL_ACTIVE); }
            OnStart() is called as soon as the program starts, OnStop() is called when a button captioned ""stop" is pressed. I press the "stop" button and my program freezes. I used debugger and found out that the thread never ended, even though g_bAbort has been set to TRUE. Why so? Thank you a lot.

            P Offline
            P Offline
            Peter Occil
            wrote on last edited by
            #5

            In the OnStart handler, you should start the thread, then loop the PeekMessage function until the thread exits or the user presses STOP.

            void CThreadTestDlg::OnStart(){
            // begin the thread
            MSG msg;
            DWORD threadec;
            g_bAbort = FALSE;
            m_pThread = ::AfxBeginThread(ThreadProc, NULL);
            while(::PeekMessage(&msg,NULL,0,0,PM_REMOVE)){
            // process any messages while the other thread does background
            // processing
            if(msg.message==WM_QUIT){ // quit this thread.
            // Add code to break the other thread here.
            ::PostQuitMessage(msg.wParam);
            break;
            }
            ::TranslateMessage(&msg);
            ::DispatchMessage(&msg);
            if(!::GetExitCodeThread(m_pThread->m_hThread,&threadec))
            return;
            if(threadec!=STILL_ACTIVE)break;
            }
            }

            I apologize it this isn't right; I'm much used to programming in C. Peter O.

            1 Reply Last reply
            0
            • S Scott H Settlemier

              What's the return value of GetExitCodeThread?

              A Offline
              A Offline
              Abin
              wrote on last edited by
              #6

              It returns TRUE, and the exit code keeps being STILL_ACTIVE even though I had set g_bAbort to FALSE already.

              1 Reply Last reply
              0
              • A Abin

                I'm studying multi-threading today and got some major problems, the biggest one is that the worker thread is not terminating as expected, my code looks like this:// global BOOL g_bAbort = FALSE; UINT ThreadProc(LPVOID lpParam) { while (!g_bAbort) { // DoSomething(); // do some thing } return 0; } // in some class named "CThreadTestDlg" void CThreadTestDlg::OnStart() { // begin the thread g_bAbort = FALSE; m_pThread = ::AfxBeginThread(ThreadProc, NULL); } void CThreadTestDlg::OnStop() { // wanna stop the thread... g_bAbort = TRUE; DWORD dwExitCode = 0; do { Sleep(200); ::GetExitCodeThread(m_pThread->m_hThread, &dwExitCode); } while (dwExitCode == STILL_ACTIVE); }
                OnStart() is called as soon as the program starts, OnStop() is called when a button captioned ""stop" is pressed. I press the "stop" button and my program freezes. I used debugger and found out that the thread never ended, even though g_bAbort has been set to TRUE. Why so? Thank you a lot.

                R Offline
                R Offline
                RichardWdy
                wrote on last edited by
                #7

                I think you should do like this... void CThreadTestDlg::OnStop() { // wanna stop the thread... g_bAbort = TRUE; DWORD dwExitCode = 0; } Thus you can abort the thread. No pains, no gains.

                R 1 Reply Last reply
                0
                • R RichardWdy

                  I think you should do like this... void CThreadTestDlg::OnStop() { // wanna stop the thread... g_bAbort = TRUE; DWORD dwExitCode = 0; } Thus you can abort the thread. No pains, no gains.

                  R Offline
                  R Offline
                  RichardWdy
                  wrote on last edited by
                  #8

                  I think you should do like this... void CThreadTestDlg::OnStop() { // wanna stop the thread... g_bAbort = TRUE; } Thus you can abort the thread. No pains, no gains.

                  1 Reply Last reply
                  0
                  • A Abin

                    I'm studying multi-threading today and got some major problems, the biggest one is that the worker thread is not terminating as expected, my code looks like this:// global BOOL g_bAbort = FALSE; UINT ThreadProc(LPVOID lpParam) { while (!g_bAbort) { // DoSomething(); // do some thing } return 0; } // in some class named "CThreadTestDlg" void CThreadTestDlg::OnStart() { // begin the thread g_bAbort = FALSE; m_pThread = ::AfxBeginThread(ThreadProc, NULL); } void CThreadTestDlg::OnStop() { // wanna stop the thread... g_bAbort = TRUE; DWORD dwExitCode = 0; do { Sleep(200); ::GetExitCodeThread(m_pThread->m_hThread, &dwExitCode); } while (dwExitCode == STILL_ACTIVE); }
                    OnStart() is called as soon as the program starts, OnStop() is called when a button captioned ""stop" is pressed. I press the "stop" button and my program freezes. I used debugger and found out that the thread never ended, even though g_bAbort has been set to TRUE. Why so? Thank you a lot.

                    T Offline
                    T Offline
                    Tomasz Sowinski
                    wrote on last edited by
                    #9
                    1. you should use WaitForSingleObject instead of Sleep/GetExitCodeThread in OnStop. 2) try marking g_bAbort with 'volatile' modifier Tomasz Sowinski -- http://www.shooltz.com

                    - It's for protection
                    - Protection from what? Zee Germans?

                    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