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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Help with threads

Help with threads

Scheduled Pinned Locked Moved C / C++ / MFC
helpcomdata-structuresjson
8 Posts 4 Posters 1 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.
  • F Offline
    F Offline
    FISH786
    wrote on last edited by
    #1

    I am trying to follow this: [Creating Threads using the CreateThread() API](<a href=)[^]"> However I am having an issue, My thread is still running even though the main process id done. What I am trying to achive is a loop to create threads and wait for all the threads to finish the process before I run the same process again. My Issue is I get a message to say my loop is done, however the thread is still running. Could someone please shed some light.

    DWORD WINAPI Thread_1(LPVOID lpParam)
    {
    int a =0;
    char Temp[256];
    THREADDATA* pData = (THREADDATA*) lpParam;
    UINT nEid = pData->nEditId;
    CThreadDemoDlg* pDlg = pData->pObjDlg;
    for (a = 0;a<20;a++)
    {
    _itoa_s(a,Temp,sizeof(Temp),10);
    pDlg->SetDlgItemText(nEid,Temp);
    ProcessMessages();
    }
    return 0;
    }

    DWORD WINAPI Thread_2(LPVOID lpParam)
    {
    int a =0;
    char Temp[256];
    THREADDATA* pData = (THREADDATA*) lpParam;
    UINT nEid = pData->nEditId;
    CThreadDemoDlg* pDlg = pData->pObjDlg;
    for (a = 0;a<20;a++)
    {
    _itoa_s(a+1,Temp,sizeof(Temp),10);
    pDlg->SetDlgItemText(nEid,Temp);
    Sleep(1000);
    ProcessMessages();
    }
    return 0;
    }

    TD1->pObjDlg = this;
    TD2->pObjDlg = this;
    TD1->nEditId = IDC_STATIC1;
    TD2->nEditId = IDC_STATIC2;

    HANDLE hThread_1;
    HANDLE hThread_2;

    HANDLE Array_Thread[2];

    for (x=0 x<10;x++)
    {
    hThread_1 = CreateThread(NULL,0,Thread_1,TD1,0,NULL);
    if (hThread_1 == NULL)
    ExitProcess(-1);
    hThread_2 = CreateThread(NULL,0,Thread_2,TD2,0,NULL);
    if (hThread_2 == NULL)
    ExitProcess(-1);
    ProcessMessages();
    x++;
    _itoa_s(f,Temp,sizeof(Temp),10);
    SetDlgItemText(IDC_Main,Temp);
    Array_Thread[0]= hThread_1;
    Array_Thread[1]= hThread_2;

        WaitForMultipleObjects(2,Array\_Thread,TRUE,INFINITE);
        CloseHandle(hThread\_1);
        CloseHandle(hThread\_2);
        //The aim is to wait for the unctions to finish before any of the threads start again
    

    }
    MessageBox("Done","Information",MB_OKCANCEL);

    I get the messagebox, however the The second thread is still running.

    _ R R 3 Replies Last reply
    0
    • F FISH786

      I am trying to follow this: [Creating Threads using the CreateThread() API](<a href=)[^]"> However I am having an issue, My thread is still running even though the main process id done. What I am trying to achive is a loop to create threads and wait for all the threads to finish the process before I run the same process again. My Issue is I get a message to say my loop is done, however the thread is still running. Could someone please shed some light.

      DWORD WINAPI Thread_1(LPVOID lpParam)
      {
      int a =0;
      char Temp[256];
      THREADDATA* pData = (THREADDATA*) lpParam;
      UINT nEid = pData->nEditId;
      CThreadDemoDlg* pDlg = pData->pObjDlg;
      for (a = 0;a<20;a++)
      {
      _itoa_s(a,Temp,sizeof(Temp),10);
      pDlg->SetDlgItemText(nEid,Temp);
      ProcessMessages();
      }
      return 0;
      }

      DWORD WINAPI Thread_2(LPVOID lpParam)
      {
      int a =0;
      char Temp[256];
      THREADDATA* pData = (THREADDATA*) lpParam;
      UINT nEid = pData->nEditId;
      CThreadDemoDlg* pDlg = pData->pObjDlg;
      for (a = 0;a<20;a++)
      {
      _itoa_s(a+1,Temp,sizeof(Temp),10);
      pDlg->SetDlgItemText(nEid,Temp);
      Sleep(1000);
      ProcessMessages();
      }
      return 0;
      }

      TD1->pObjDlg = this;
      TD2->pObjDlg = this;
      TD1->nEditId = IDC_STATIC1;
      TD2->nEditId = IDC_STATIC2;

      HANDLE hThread_1;
      HANDLE hThread_2;

      HANDLE Array_Thread[2];

      for (x=0 x<10;x++)
      {
      hThread_1 = CreateThread(NULL,0,Thread_1,TD1,0,NULL);
      if (hThread_1 == NULL)
      ExitProcess(-1);
      hThread_2 = CreateThread(NULL,0,Thread_2,TD2,0,NULL);
      if (hThread_2 == NULL)
      ExitProcess(-1);
      ProcessMessages();
      x++;
      _itoa_s(f,Temp,sizeof(Temp),10);
      SetDlgItemText(IDC_Main,Temp);
      Array_Thread[0]= hThread_1;
      Array_Thread[1]= hThread_2;

          WaitForMultipleObjects(2,Array\_Thread,TRUE,INFINITE);
          CloseHandle(hThread\_1);
          CloseHandle(hThread\_2);
          //The aim is to wait for the unctions to finish before any of the threads start again
      

      }
      MessageBox("Done","Information",MB_OKCANCEL);

      I get the messagebox, however the The second thread is still running.

      _ Offline
      _ Offline
      _Superman_
      wrote on last edited by
      #2

      How are you validating that the second thread is running?

      «_Superman_» I love work. It gives me something to do between weekends.

      F 1 Reply Last reply
      0
      • F FISH786

        I am trying to follow this: [Creating Threads using the CreateThread() API](<a href=)[^]"> However I am having an issue, My thread is still running even though the main process id done. What I am trying to achive is a loop to create threads and wait for all the threads to finish the process before I run the same process again. My Issue is I get a message to say my loop is done, however the thread is still running. Could someone please shed some light.

        DWORD WINAPI Thread_1(LPVOID lpParam)
        {
        int a =0;
        char Temp[256];
        THREADDATA* pData = (THREADDATA*) lpParam;
        UINT nEid = pData->nEditId;
        CThreadDemoDlg* pDlg = pData->pObjDlg;
        for (a = 0;a<20;a++)
        {
        _itoa_s(a,Temp,sizeof(Temp),10);
        pDlg->SetDlgItemText(nEid,Temp);
        ProcessMessages();
        }
        return 0;
        }

        DWORD WINAPI Thread_2(LPVOID lpParam)
        {
        int a =0;
        char Temp[256];
        THREADDATA* pData = (THREADDATA*) lpParam;
        UINT nEid = pData->nEditId;
        CThreadDemoDlg* pDlg = pData->pObjDlg;
        for (a = 0;a<20;a++)
        {
        _itoa_s(a+1,Temp,sizeof(Temp),10);
        pDlg->SetDlgItemText(nEid,Temp);
        Sleep(1000);
        ProcessMessages();
        }
        return 0;
        }

        TD1->pObjDlg = this;
        TD2->pObjDlg = this;
        TD1->nEditId = IDC_STATIC1;
        TD2->nEditId = IDC_STATIC2;

        HANDLE hThread_1;
        HANDLE hThread_2;

        HANDLE Array_Thread[2];

        for (x=0 x<10;x++)
        {
        hThread_1 = CreateThread(NULL,0,Thread_1,TD1,0,NULL);
        if (hThread_1 == NULL)
        ExitProcess(-1);
        hThread_2 = CreateThread(NULL,0,Thread_2,TD2,0,NULL);
        if (hThread_2 == NULL)
        ExitProcess(-1);
        ProcessMessages();
        x++;
        _itoa_s(f,Temp,sizeof(Temp),10);
        SetDlgItemText(IDC_Main,Temp);
        Array_Thread[0]= hThread_1;
        Array_Thread[1]= hThread_2;

            WaitForMultipleObjects(2,Array\_Thread,TRUE,INFINITE);
            CloseHandle(hThread\_1);
            CloseHandle(hThread\_2);
            //The aim is to wait for the unctions to finish before any of the threads start again
        

        }
        MessageBox("Done","Information",MB_OKCANCEL);

        I get the messagebox, however the The second thread is still running.

        R Offline
        R Offline
        Roger Stoltz
        wrote on last edited by
        #3

        Phweeew... To be honest, there's a lot of things wrong with your code and how you think you should go about multithreading. Let's try and pin-point a few.

        • Use AfxBeginThread() when building with MFC framework This must be done to keep the framework happy. Otherwise you'll get into obscure problems that are hard to track down.
        • Do NOT touch the GUI from a secondary thread This is very important, especially in MFC since e.g. SetDlgItemText() sends a message to the windows of the control. The difference between sending and posting a message is that sending will block until the message has been processed by the receiving thread. This means that if the thread that "owns" the window doesn't process messages, you'll get a nice little deadlock.
        • Worker threads doesn't process messages Your calls to ProcessMessages() are, apart from completely unnecessary, fundamentally wrong. No messages will be sent to your threads. You need to differentiate between so called worker threads that doesn't process messsages and UI-threads.
        • Your main thread doesn't process messages After you have called ProcessMessages(), your main thread doesn't process messages any longer. This means that your secondary threads will hang when calling SetDlgItemText() since the receiving window/thread will not consume the message. One way to solve it would be to use ::MsgWaitForMultipleObjects() while waiting for the threads to complete. The correct way would be to return from the function that creates the threads and revert to the window procedure for the main thread. You get into this kind of trouble since you're trying to learn multithreading, but you're using it in a way that contradicts with the reason for having multiple threads since you're still doing things sequentially.

        I suspect that the reason why your thread #2 is "still running" when you get the message box is a combination of the above. In other words: your thread #2 hangs because of the call to ::Sleep() since by the time it resumes execution your main thread has stopped processing messages. You should really have a look at the return value from ::WaitForMultipleObjects(). Some words about what you should do.... :-\ Forget about the article you referred to and start here[

        F 1 Reply Last reply
        0
        • F FISH786

          I am trying to follow this: [Creating Threads using the CreateThread() API](<a href=)[^]"> However I am having an issue, My thread is still running even though the main process id done. What I am trying to achive is a loop to create threads and wait for all the threads to finish the process before I run the same process again. My Issue is I get a message to say my loop is done, however the thread is still running. Could someone please shed some light.

          DWORD WINAPI Thread_1(LPVOID lpParam)
          {
          int a =0;
          char Temp[256];
          THREADDATA* pData = (THREADDATA*) lpParam;
          UINT nEid = pData->nEditId;
          CThreadDemoDlg* pDlg = pData->pObjDlg;
          for (a = 0;a<20;a++)
          {
          _itoa_s(a,Temp,sizeof(Temp),10);
          pDlg->SetDlgItemText(nEid,Temp);
          ProcessMessages();
          }
          return 0;
          }

          DWORD WINAPI Thread_2(LPVOID lpParam)
          {
          int a =0;
          char Temp[256];
          THREADDATA* pData = (THREADDATA*) lpParam;
          UINT nEid = pData->nEditId;
          CThreadDemoDlg* pDlg = pData->pObjDlg;
          for (a = 0;a<20;a++)
          {
          _itoa_s(a+1,Temp,sizeof(Temp),10);
          pDlg->SetDlgItemText(nEid,Temp);
          Sleep(1000);
          ProcessMessages();
          }
          return 0;
          }

          TD1->pObjDlg = this;
          TD2->pObjDlg = this;
          TD1->nEditId = IDC_STATIC1;
          TD2->nEditId = IDC_STATIC2;

          HANDLE hThread_1;
          HANDLE hThread_2;

          HANDLE Array_Thread[2];

          for (x=0 x<10;x++)
          {
          hThread_1 = CreateThread(NULL,0,Thread_1,TD1,0,NULL);
          if (hThread_1 == NULL)
          ExitProcess(-1);
          hThread_2 = CreateThread(NULL,0,Thread_2,TD2,0,NULL);
          if (hThread_2 == NULL)
          ExitProcess(-1);
          ProcessMessages();
          x++;
          _itoa_s(f,Temp,sizeof(Temp),10);
          SetDlgItemText(IDC_Main,Temp);
          Array_Thread[0]= hThread_1;
          Array_Thread[1]= hThread_2;

              WaitForMultipleObjects(2,Array\_Thread,TRUE,INFINITE);
              CloseHandle(hThread\_1);
              CloseHandle(hThread\_2);
              //The aim is to wait for the unctions to finish before any of the threads start again
          

          }
          MessageBox("Done","Information",MB_OKCANCEL);

          I get the messagebox, however the The second thread is still running.

          R Offline
          R Offline
          Rajesh R Subramanian
          wrote on last edited by
          #4

          FISH786 wrote:

          CThreadDemoDlg* pDlg = pData->pObjDlg;

          I smell MFC?

          FISH786 wrote:

          hThread_2 = CreateThread(NULL,0,Thread_2,TD2,0,NULL);

          And you're calling CreateThread(). First things first[^]

          It is a crappy thing, but it's life -^ Carlo Pallini

          R 1 Reply Last reply
          0
          • R Roger Stoltz

            Phweeew... To be honest, there's a lot of things wrong with your code and how you think you should go about multithreading. Let's try and pin-point a few.

            • Use AfxBeginThread() when building with MFC framework This must be done to keep the framework happy. Otherwise you'll get into obscure problems that are hard to track down.
            • Do NOT touch the GUI from a secondary thread This is very important, especially in MFC since e.g. SetDlgItemText() sends a message to the windows of the control. The difference between sending and posting a message is that sending will block until the message has been processed by the receiving thread. This means that if the thread that "owns" the window doesn't process messages, you'll get a nice little deadlock.
            • Worker threads doesn't process messages Your calls to ProcessMessages() are, apart from completely unnecessary, fundamentally wrong. No messages will be sent to your threads. You need to differentiate between so called worker threads that doesn't process messsages and UI-threads.
            • Your main thread doesn't process messages After you have called ProcessMessages(), your main thread doesn't process messages any longer. This means that your secondary threads will hang when calling SetDlgItemText() since the receiving window/thread will not consume the message. One way to solve it would be to use ::MsgWaitForMultipleObjects() while waiting for the threads to complete. The correct way would be to return from the function that creates the threads and revert to the window procedure for the main thread. You get into this kind of trouble since you're trying to learn multithreading, but you're using it in a way that contradicts with the reason for having multiple threads since you're still doing things sequentially.

            I suspect that the reason why your thread #2 is "still running" when you get the message box is a combination of the above. In other words: your thread #2 hangs because of the call to ::Sleep() since by the time it resumes execution your main thread has stopped processing messages. You should really have a look at the return value from ::WaitForMultipleObjects(). Some words about what you should do.... :-\ Forget about the article you referred to and start here[

            F Offline
            F Offline
            FISH786
            wrote on last edited by
            #5

            Thank you for the feedback. I appreciate the honesty. I will check on the article and work my way from there. thanks a million.

            1 Reply Last reply
            0
            • R Rajesh R Subramanian

              FISH786 wrote:

              CThreadDemoDlg* pDlg = pData->pObjDlg;

              I smell MFC?

              FISH786 wrote:

              hThread_2 = CreateThread(NULL,0,Thread_2,TD2,0,NULL);

              And you're calling CreateThread(). First things first[^]

              It is a crappy thing, but it's life -^ Carlo Pallini

              R Offline
              R Offline
              Roger Stoltz
              wrote on last edited by
              #6

              Rajesh R Subramanian wrote:

              FISH786 wrote:

              CThreadDemoDlg* pDlg = pData->pObjDlg;

              I smell MFC?

              FISH786 wrote:

              MessageBox("Done","Information",MB_OKCANCEL);

              Calling MessageBox() without a window handle looks very MFC-ish to me, so I guess we are right in our assumptions... ;)

              "It's supposed to be hard, otherwise anybody could do it!" - selfquote
              "High speed never compensates for wrong direction!" - unknown

              R 1 Reply Last reply
              0
              • R Roger Stoltz

                Rajesh R Subramanian wrote:

                FISH786 wrote:

                CThreadDemoDlg* pDlg = pData->pObjDlg;

                I smell MFC?

                FISH786 wrote:

                MessageBox("Done","Information",MB_OKCANCEL);

                Calling MessageBox() without a window handle looks very MFC-ish to me, so I guess we are right in our assumptions... ;)

                "It's supposed to be hard, otherwise anybody could do it!" - selfquote
                "High speed never compensates for wrong direction!" - unknown

                R Offline
                R Offline
                Rajesh R Subramanian
                wrote on last edited by
                #7

                So, we can confirm the crime. :suss: But, I didn't go that far. I saw the CThreadDemoDlg* on the first few lines and assumed MFC. After all, the user (while sober) cannot come up with a funky name of that sort on his own. :)

                It is a crappy thing, but it's life -^ Carlo Pallini

                1 Reply Last reply
                0
                • _ _Superman_

                  How are you validating that the second thread is running?

                  «_Superman_» I love work. It gives me something to do between weekends.

                  F Offline
                  F Offline
                  FISH786
                  wrote on last edited by
                  #8

                  According to the article. The array I pass of handles to the WaitMultipleObjects should take care of that for me.

                  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